Django实现登录验证功能

Django实现登录验证功能
Django实现登录验证功能:
Django对用户登录功能已经进行了封装,我们只需要简单地修改就可以了。
视图:
views.py

# Create your views here.
# -*- coding: utf-8 -*-
from django.shortcuts import render,render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.template import RequestContext
from webserver.forms import UserForm,RegisterForm
import time


#登录验证
def login(req):
    nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    if req.method == 'GET':
        uf = UserForm()
        return render_to_response('login.html', RequestContext(req, {'uf': uf,'nowtime': nowtime }))
    else:
        uf = UserForm(req.POST)
        if uf.is_valid():
            username = req.POST.get('username', '')
            password = req.POST.get('password', '')
            user = auth.authenticate(username = username,password = password)
            if user is not None and user.is_active:
                auth.login(req,user)
                return render_to_response('index.html', RequestContext(req))
            else:
                return render_to_response('login.html', RequestContext(req, {'uf': uf,'nowtime': nowtime, 'password_is_wrong': True}))
        else:
            return render_to_response('login.html', RequestContext(req, {'uf': uf,'nowtime': nowtime }))

路由:

urls.py

from django.conf.urls import *
from webserver import views

urlpatterns = [
    url(r'^login/$',views.login),
]



html页面
login.html
{% load staticfiles %}
<link href="{% static "css/adstyle.css"%}" rel="stylesheet" type="text/css" />
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login</title>
</head>
{% if password_is_wrong %}
    <script type="text/javascript" src="{%static "js/jquery-1.11.0.min.js" %}"></script>
    <script type="text/javascript" src="{%static "js/alert.js" %}"></script>
    <link href="{%static "css/alert.css" %}" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        Alert.showMsg("错误!!用户名或密码错误!");
        location.href="/webserver/login/"
    </script>
{% endif %}
<body>
    <div class="admin-ht" style="background: url(/static/images/lo.jpg);">
        <div class="adminBord">
            <h1>运维管理平台</h1>
        <h4>{{ nowtime }}</h4>
            <form method="post" enctype="multipart/form-data" >
                {% csrf_token %}
                {{uf.as_p}}
                <input type="submit" value="login" id="loging">
            </form>
        </div>
    </div>
</body>
</html>


效果:


效果:




  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值