Devinwon / article

0 stars 0 forks source link

Django2.0 ——login example #12

Open Devinwon opened 6 years ago

Devinwon commented 6 years ago

使用系统自带的表单,既然使用了框架,就尽可能使用框架提供的功能,我的位于login/forms.py, 这里使用了默认的用户名登录,如果需要使用其他方式登录,需要另行设置,这里暂不做说明。

from django import forms
class Logfm(forms.Form):
    username = forms.CharField(
    widget=forms.TextInput(attrs={"placeholder": "输入用户名", }),
    error_messages={"required": "用户名不能为空",},
    label="输入用户")

    password_set = forms.CharField(
    widget=forms.PasswordInput(attrs={"placeholder": "输入密码",}),
    error_messages={"required": "密码不能为空",},
    label="输入密码")

视图处理函数,login/views.py

from django.shortcuts import render,redirect
from django.contrib.auth.models import User
from django.contrib import auth
from django.http import HttpResponse,HttpResponseRedirect
from django.template import Template,Context
from login.forms import Logfm
def login(request):
    context={}
    logfm=Logfm()
    context["logfm"]=logfm
    if request.method=="GET":
        return render(request,"login.html",context)
    else:
        try:
            logfm=Logfm(request.POST)
            if logfm.is_valid():
                username=request.POST.get("username")
                password_set=request.POST.get("password_set")
                usernameResult = User.objects.filter(username__exact=username)
                #user = auth.authenticate(username=username, password=password)
                if usernameResult:
                    # userResult = User.objects.filter(username__exact=username,password__exact=password_set)
                    userResult = auth.authenticate(username=username, password=password_set)
                    if userResult and userResult.is_active:
                        response=redirect(to='/reg')
                        #或者
                                                #response= HttpResponseRedirect("/reg")
                        response.set_cookie("username",username,max_age=3600)
                        return response

                    else:
                        err="用户名与密码不匹配"
                        context["err"]=err
                        return render(request,"login.html",context)
                else:
                    err="用户名"+username+"不存在"
                    context["err"]=err
            return render(request,'login.html',context)
        except:
            return HttpResponse("login error,contact with master ")

模板当中的示例代码,我的位于login/templates/login.html

    <div class="ui form">
      <form action="/account/login/" method="POST" class="" id="regfm" >

        {% csrf_token %}
        {% for field in logfm %}
          <div  id="regfield" class="">
            {{ field.label_tag }}{{ field }} 
            {{ field.errors}}
          </div>
        {% endfor %}
          <p id="regerr">
            {% if err %}
            <i class="warning circle icon red"></i>{{ err }}
            {% endif %}
          </p>
        <input type="submit" value="登录" class="ui green button">
        <input type="reset" value="重置" class="ui gray button">
      </form>
    </div>

示例效果 login