美文网首页
django2登录注册功能学习笔记

django2登录注册功能学习笔记

作者: 假装我不帅 | 来源:发表于2019-02-16 20:23 被阅读0次

    参考链接

    django2.1登录注册笔记

    1. 准备

    django-admin startproject auth
    django-admin startapp myauth
    settings.py注册myauth app
    auth下urls.py,引入inlcude,添加一行代码

    path('',include('myauth.urls')),
    

    myauth下新建urls.py ,添加如下代码:

    from django.urls import path
    from . import views
    urlpatterns = [
        path('',views.index,name='index'),#index=主页
        
    ]
    

    打开myauth 下的views.py,添加如下代码:

    from django.shortcuts import render
    # Create your views here.
    def index(request):
        return render(request,'myauth/home.html')
    

    在myauth文件夹下新建templates文件夹,在此文件夹下新建myauth文件夹,在myauth文件夹下新建home.html
    写入一下代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>主页</title>
    </head>
    <body>
    <h1>主页</h1>
    </body>
    </html>
    

    访问http://127.0.0.1:8000/
    就看到两个字主页

    2. 登录页面

    myauth app 下的urls.py添加如下代码:

    path('login/',views.mylogin,name="login"),#login=登录
    

    myauth app下views.py 新建函数,添加如下代码:

    def login(request):
        return render(request,'myauth/login.html')
    

    同建立home.html一样建立login.html,写入如下代码:

    <!DOCTYPE html>
    <html lang="zh-hans">
    <head>
        <meta charset="UTF-8">
        <title>登录界面</title>
    </head>
    <body>
    <a href="{% url 'myauth:index'  %}">主页</a>
    {{ error }}
    <form action="" method="post">
        {% csrf_token %}
        <input type="text" name="username" placeholder="请输入用户名">
        <input type="password" name="password">
        <br>
        <button type="submit">提交</button>
        
    </form>
    </body>
    </html>
    

    访问:http://127.0.0.1:8000/login/
    页面如下图所示:


    在myauth app下的urls.py添加一行代码:
    app_name='myauth'
    

    home.html添加一行代码:

    <a href="{% url 'myauth:login' %}">登录</a>
    

    3.登录的后端逻辑

    python manage.py migtrate
    

    打开myauth app下的views.py代码,修改mylogin函数代码如下:

    from django.shortcuts import render,redirect
    def mylogin(request):
        if request.method =="POST":
            username=request.POST.get('username')
            password=request.POST.get('password')
            user=authenticate(request,username=username,password=password)
            if user is None:
                return render(request,'myauth/login.html',{'error':"用户名不存在"})
            else:
                login(request,user)
                return redirect('myauth:index')
    
        else:
            return render(request,'myauth/login.html')
    
    python manage.py createsuperuser
    

    访问http://127.0.0.1:8000/login/
    输入创建的用户名和密码
    就可以返回到主页

    4. 登出

    home.html添加如下代码:

    <a href="{% url 'myauth:logout' %}">注销</a>
    

    在myauth app下的urls.py添加如下代码:

    path('logout',views.mylogout,name="logout"),# logout=登出
    

    在myauth app下的views.py添加如下代码:

    def mylogout(request):
        logout(request)
        return redirect('myauth:index')
    

    修改home.html代码:

    <!DOCTYPE html>
    <html lang="zh-hans">
    <head>
        <meta charset="UTF-8">
        <title>登录界面</title>
    </head>
    <body>
    <a href="{% url 'myauth:index'  %}">主页</a>
    <form action="" method="post">
        {% csrf_token %}
        <input type="text" name="username" placeholder="请输入用户名">
        <input type="password" name="password">
        <br>
        <button type="submit">提交</button>
    </form>
    </body>
    </html>
    

    4. 注册

    myauth app下的urls.py 添加一行代码:

    path('registerr/',views.myregister,name="register"),#register=注册
    

    在home.html下的<a href="{% url 'myauth:login' %}">登录</a>后面添加一行代码:

    <a href="{% url 'myauth:register' %}">注册</a>
    

    myauth app下views.py添加如下代码:

    from django.contrib.auth.forms import UserCreationForm
    def myregister(request):
        context={}
        registerForm=UserCreationForm()
        context["registerForm"]=registerForm
        return render(request,'myauth/register.html',context)
    

    根据前面的步骤建立register.html,里面写入如下代码:

    <!DOCTYPE html>
    <html lang="zh-hans">
    <head>
        <meta charset="UTF-8">
        <title>注册</title>
    </head>
    <body>
    <h1>注册</h1>
    <a href="{% url 'myauth:index'  %}">主页</a>
    
    <form action="" method="post">
        {% csrf_token %}
        {{ registerForm.as_p }}
        <button type="submit">注册</button>
    </form>
    </body>
    </html>
    

    访问:http://127.0.0.1:8000/register/


    修改settings.py
    LANGUAGE_CODE = 'zh-hans'
    
    TIME_ZONE = 'Asia/Shanghai'
    

    myauth app下的urls.py修改myresiter函数代码:

    def myregister(request):
        if request.method=="POST":
            registerForm=UserCreationForm(request.POST)
            if registerForm.is_valid():
                registerForm.save()
                user=authenticate(username=registerForm.cleaned_data["username"],password=registerForm.cleaned_data["password1"])
                login(request,user)
                return redirect('myauth:index')
        else:
            registerForm=UserCreationForm()
        context = {}
        context["registerForm"]=registerForm
        return render(request,'myauth/register.html',context)
    

    错误示例




    成功则返回到首页

    5. 自定义表单

    5. 自定义表单

    拓展模型,一对一关系

    打开myauth app下的models.py
    写入一下代码:

    from django.db import models
    from django.contrib.auth.models import User
    # Create your models here.
    class formalVIP(models.Model):
        user=models.OneToOneField(User,on_delete=models.CASCADE)
        nick=models.CharField(blank=True,max_length=50)
        birthday=models.DateField(blank=True,null=True)
        class Meta:
            verbose_name_plural="普通会员表"#指定模型的复数形式
    

    运行迁移命令:

    python manage.py makemigrations
    python manage.py migrate
    

    打开myauth app下的admin.py,写入一下代码:

    from django.contrib import admin
    from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
    from django.contrib.auth.models import User
    from .models import formalVIP
    # Register your models here.
    class formalVIPInline(admin.TabularInline):
        model = formalVIP
        can_delete = False
        verbose_name_plural = "普通会员表"
    class UserAdmin(BaseUserAdmin):
        inlines = (formalVIPInline,)
    
    admin.site.unregister(User)
    admin.site.register(User,UserAdmin)
    

    myauth app下新建forms.py,添加如下代码:

    from django.contrib.auth.forms import UserCreationForm
    from django import forms
    from django.contrib.auth.models import User
    class DIYUserCreationForm(UserCreationForm):
        #自定义注册表单
        nick=forms.CharField(required=False,max_length=50)
        birthday = forms.DateField(required=False)
        class Meta:
            model=User
            fields=('__all__')
    

    使用forms
    修改myauth下的views.py的代码:

    from .forms import DIYUserCreationForm
    # 把所有registerForm=UserCreationForm(request.POST)改为
    registerForm=DIYUserCreationForm(request.POST)
    

    修改fields为:

    fields=('nick','birthday')
    

    修改forms.py

    class DIYUserCreationForm(UserCreationForm):
        #自定义注册表单
        昵称=forms.CharField(required=False,max_length=50)
        生日 = forms.DateField(required=False)
        class Meta:
            model=User
            fields=('username','password1','password2','email','昵称','生日')
    

    打开myauth app下views.py

    from .models import formalVIP
    

    如下图所示的位置添加如下代码:

    user.email=registerForm.cleaned_data['email']
    formalVIP(user=user,nick=registerForm.cleaned_data['昵称'],birthday=registerForm.cleaned_data['生日']).save()#models.py models=views
    

    注册用户,填写表单


    打开网址查看http://127.0.0.1:8000/admin/auth/user/3/change/
    用户昵称是有的

    修改日期样式

    查看注册的网页源代码,把form表单的代码粘贴到register.html中

    {% if registerForm.errors %}
        {{ registerForm.errors }}
    {% endif %}
    <form action="" method="post">
        {% csrf_token %}
        <p><label for="id_username">用户名:</label> <input type="text" name="username" maxlength="150" autofocus required id="id_username"> <span class="helptext">必填。150个字符或者更少。包含字母,数字和仅有的@/./+/-/_符号。</span></p>
        <p><label for="id_password1">密码:</label> <input type="password" name="password1" required id="id_password1"> <span class="helptext"><ul><li>你的密码不能与其他个人信息太相似。</li><li>你的密码必须包含至少 8 个字符。</li><li>你的密码不能是大家都爱用的常见密码。</li><li>你的密码不能全部为数字。</li></ul></span></p>
        <p><label for="id_password2">密码确认:</label> <input type="password" name="password2" required id="id_password2"> <span class="helptext">为了校验,请输入与上面相同的密码。</span></p>
        <p><label for="id_email">电子邮件地址:</label> <input type="email" name="email" maxlength="254" id="id_email"></p>
        <p><label for="id_昵称">昵称:</label> <input type="text" name="昵称" maxlength="50" id="id_昵称"></p>
        <p><label for="id_生日">生日:</label> <input type="date" name="生日" id="id_生日"></p>
        <button type="submit">注册</button>
    </form>
    

    把先前自己写的form表单删除
    填写注册表单,如下图



    自定义错误提示

    打开register.html,删除一下代码:

    {% if registerForm.errors %}
        
    {% endif %}
    

    修改register.html代码为:

    <!DOCTYPE html>
    <html lang="zh-hans">
    <head>
        <meta charset="UTF-8">
        <title>注册</title>
    </head>
    <body>
    <h1>注册</h1>
    <a href="{% url 'myauth:index'  %}">主页</a>
    <form action="" method="post">
        {% csrf_token %}
        <p>
            <label for="id_username">用户名:</label>
            <input type="text" name="username" maxlength="150" autofocus required id="id_username">
            <span class="helptext">必填。150个字符或者更少。包含字母,数字和仅有的@/./+/-/_符号。</span>
        </p>
        <div>{{ registerForm.errors.username }}</div>
        <p>
            <label for="id_password1">密码:</label>
            <input type="password" name="password1" required id="id_password1">
            <span class="helptext">
                <ul>
                    <li>你的密码不能与其他个人信息太相似。</li>
                    <li>你的密码必须包含至少 8 个字符。</li>
                    <li>你的密码不能是大家都爱用的常见密码。</li>
                    <li>你的密码不能全部为数字。</li>
                </ul>
            </span>
        </p>
        <div>{{ registerForm.errors.password1}}</div>
        <p>
            <label for="id_password2">密码确认:</label>
            <input type="password" name="password2" required id="id_password2">
            <span class="helptext">为了校验,请输入与上面相同的密码。</span>
        </p>
        <div>{{ registerForm.errors.password2}}</div>
        <p>
            <label for="id_email">电子邮件地址:</label>
            <input type="email" name="email" maxlength="254" id="id_email">
        </p>
        <div>{{ registerForm.errors.email}}</div>
        <p>
            <label for="id_昵称">昵称:</label>
            <input type="text" name="昵称" maxlength="50" id="id_昵称">
        </p>
        <div>{{ registerForm.errors.昵称}}</div>
        <p>
            <label for="id_生日">生日:</label>
            <input type="date" name="生日" id="id_生日">
        </p>
        <div>{{ registerForm.errors.生日}}</div>
        <button type="submit">注册</button>
    </form>
    </body>
    </html>
    

    填错后界面显示如下:


    <div>{{ registerForm.errors.password1}}</div>修改为如下代码:
    {% if registerForm.errors.username %}
        <p style="color: red;">用户名已经存在或者格式不符合要求</p>
    {% endif %}
    

    填写相同的用户名,界面如下:



    打开forms.py表单,在DIYCetationForm类下添加函数:

    def __init__(self,*args,**kwargs):#初始化构造函数
        super().__init__(*args,**kwargs)#上一级的初始化函数
        self.fields['username'].error_messages={'unique':'用户名已存在'}# 用户名相同产生的错误
    

    修改register.html代码,把

    {% if registerForm.errors.username %}
        <p style="color: red;">用户名已经存在或者格式不符合要求</p>
    {% endif %}
    

    改为:

    {% if registerForm.errors.username %}
        <p style="color: red;">用户名已经存在或者格式不符合要求</p>
        <p style="color: red;">{{ registerForm.errors.username }}</p>
    {% endif %}
    

    输入相同用户名,界面如下:



    添加一行代码如下:

    <p style="color: red;">{{ registerForm.errors.username.as_json }}</p>
    

    表单中填写特殊符号:"
    报错如下:



    在forms.py页面的最后一行代码添加如下键值对:

    'invalid':'格式不对'
    

    去掉register.html的刚刚添加的.as_json,重新提交查看页面:


    个人中心

    打开myauth app下的urls.py,添加三条路由:

    path('userCenter/',views.userCenter,name="userCenter"),#个人中心
    path('userCenter/editProfile',views.editorProfile,name="editorProfile"),#编辑个人信息
    path('userCenter/changePassword',views.changePassword,name="changePassword"),#修改密码
    

    打开myauth 下的views.py,添加如下代码:

    from django.contrib.auth.decorators import login_required
    #登录才可以修改查看
    @login_required(login_url='myauth:login')
    def userCenter(request):#用户中心
        return render(request,'myauth/userCenter.html')
    
    @login_required(login_url='myauth:login')
    def editorProfile(request):#编辑个人信息
        if request.method=="POST":
            return redirect('myauth:userCenter')
        else:
            return render(request,'myauth/editorProfile.html')
    
    @login_required(login_url='myauth:login')
    def changePassword(request):# 更改密码
        if request.method=="POST":
            return redirect('myauth:userCenter')
        else:
            return render(request,'myauth/changePassword.html')
    

    在myauth下的templates文件夹下的myauth文件夹下新建三个html文件,分别为
    changePassword.html
    editorProfile.html
    userCenter.html
    在home.html下添加如下代码:

    <a href="{% url 'myauth:userCenter' %}">个人中心</a>
    
    位置如下图:

    userCenter.html添加如下代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>个人中心</title>
    </head>
    <body>
    <h1>个人中心</h1>
    <a href="{% url 'myauth:index' %}">主页</a>
    <a href="{% url 'myauth:changePassword' %}">修改密码</a>
    <a href="{% url 'myauth:editorProfile' %}">编辑个人信息</a>
    <a href="{% url 'myauth:logout' %}">注销</a>
    </body>
    </html>
    

    登陆成功显示如下界面:


    点击个人中心,出现如下图所示的界面:

    未登录时输入一下网址:
    http://127.0.0.1:8000/login/?next=/userCenter/
    会自动跳转到登录页面

    修改myauth app下的views.py下的userCenter函数为
    def userCenter(request):#用户中心
        context={'user',request.user}#request.user django模型自带的User
        return render(request,'myauth/userCenter.html',context)
    

    在userCenter.html下添加如下代码:

    <div>
        <ul>
            <li>{{ user.username }}</li>
            <li>{{ user.password }}</li>
            <li>{{ user.email }}</li>
            <li>{{ user.nick }}</li>
            <li>{{ user.birthday }}</li>
        </ul>
    </div>
    

    进入个人中心,显示如下图所示的界面:



    没有昵称和生日,普通会员表时附加的
    修改代码为:

    <li>{{ user.formalvip.nick }}</li>
    <li>{{ user.formalvip.birthday }}</li>
    

    结果如下图:


    修改个人信息

    打开myauth app下的editorProfile.html,添加如下代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>修改个人信息</title>
    </head>
    <body>
    <h1>修改个人信息</h1>
    <a href="{% url 'myauth:index' %}">主页</a>
    <a href="{% url 'myauth:changePassword' %}">修改密码</a>
    <a href="{% url 'myauth:userCenter' %}">个人中心</a>
    <a href="{% url 'myauth:logout' %}">注销</a>
    <form action="" method="post">
        {% csrf_token %}
        {{ editorForm.errors }}
        {{ editorForm.as_p }}
    </form>
    </body>
    </html>
    

    打开myauth app下的views.py,修改editorProfile函数
    添加如下代码:

    from django.contrib.auth.forms import UserCreationForm,UserChangeForm
    def editorProfile(request):#编辑个人信息
        if request.method == "POST":
            editorForm = DIYUserChangeForm(request.POST,instance=request.user)#你要修改谁要写清楚
            if editorForm.is_valid():
                editorForm.save()  # 创建并保存,用户信息已经改变了
                return redirect('myauth:userCenter')
        else:
            editorForm = UserChangeForm()
        context = {}
        context["editorForm"] = editorForm
        return render(request, 'myauth/editorProfile.html', context)
    

    进入个人中心,点击编辑个人信息,出现如下界面:



    由于显示东西太多,要做修改。打开forms.py,添加如下代码:

    class DIYUserChangeForm(UserChangeForm):
        #自定义编辑表单
        nick=forms.CharField(required=False,max_length=50)
        birthday=forms.DateField(required=False)
        class Meta:
            model=User
            fields=('username','password','email','nick','birthday')
        def __init__(self,*args,**kwargs):#初始化构造函数
            super().__init__(*args,**kwargs)#上一级的初始化函数
            self.fields['username'].error_messages={'unique':'用户名已存在','invalid':'格式不对'}# 用户名相同产生的错误
    

    打开myauth app下的views.py添加引用

    from .forms import DIYUserCreationForm,DIYUserChangeForm
    

    views.py中的editorForm = UserChangeForm()改为:

    editorForm = DIYUserChangeForm()
    

    views.py中的editorForm = DIYUserChangeForm()改为:

    editorForm = DIYUserChangeForm(instance=request.user)
    

    打开编辑个人信息页面:



    昵称生日并没有自动填写,后面会编写代码
    打开editorProfile.html,添加一个提交按钮,

    <button type="submit">提交</button>
    
    修改用户的邮箱

    在myauth app下的views.py的editorProfile函数下添加如下代码:

    context["user"]=request.user
    #一定在context={}后
    

    editorProfile.html里面添加两行代码:

    {{ user.formalvip.nick }}
    {{ user.formalvip.birthday }}
    
    打开编辑个人信息页查看:

    查看网页源代码把form表单复制到editorProfile.html,最后修改代码如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>修改个人信息</title>
    </head>
    <body>
    <h1>个人中心</h1>
    <a href="{% url 'myauth:index' %}">主页</a>
    <a href="{% url 'myauth:changePassword' %}">修改密码</a>
    <a href="{% url 'myauth:userCenter' %}">个人中心</a>
    <a href="{% url 'myauth:logout' %}">注销</a>
    {{ editorForm.errors }}
    <form action="" method="post">
        {% csrf_token %}
        <p>
            <label for="id_username">用户名:</label>
            <input type="text" name="username" value="{{user.username}}" maxlength="150" required id="id_username">
            <span class="helptext">必填。150个字符或者更少。包含字母,数字和仅有的@/./+/-/_符号。</span>
        </p>
        <p>
            <label for="id_email">电子邮件地址:</label>
            <input type="email" name="email" value="{{user.email}}" maxlength="254" id="id_email">
        </p>
        <p>
            <label for="id_nick">昵称:</label>
            <input type="text" name="nick" maxlength="50" id="id_nick" value="{{ user.formalvip.nick }}">
        </p>
        <p>
            <label for="id_birthday">生日:</label>
            <input type="date" name="birthday" id="id_birthday">
        </p>
        <button type="submit">提交</button>
    </form>
    </body>
    </html>
    

    但是有一个问题是修改了昵称和生日并没有改变。
    修改myauth app下的views.py下的editorProfile函数,在第二个if条件下return前面添加如下代码:

    request.user.formalvip.nick=editorForm.cleaned_data['nick']
    request.user.formalvip.birthday = editorForm.cleaned_data['birthday']
    request.user.formalvip.save()
    

    昵称生日修改:



    修改密码

    修改changePassword.html,添加代码如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>修改个人密码</title>
    </head>
    <body>
    <h1>修改密码</h1>
    <a href="{% url 'myauth:index' %}">主页</a>
    <a href="{% url 'myauth:editorProfile' %}">修改个人信息</a>
    <a href="{% url 'myauth:userCenter' %}">个人中心</a>
    <a href="{% url 'myauth:logout' %}">注销</a>
    {{ changePwdForm.errors }}
    <form action="" method="post">
        {% csrf_token %}
        {{ changePwdForm.as_p }}
        <buttuon type="submit">提交</buttuon>
    </form>
    </body>
    </html>
    

    打开myauth app下的views.py,添加引用:

    from django.contrib.auth.forms import PasswordChangeForm
    #修改changePassword函数如下:
    def changePassword(request):# 更改密码
        if request.method == "POST":
            changePwdForm = PasswordChangeForm(data=request.POST, user=request.user)  # 你要修改谁要写清楚
            if changePwdForm.is_valid():
                changePwdForm.save()  # 创建并保存,用户信息已经改变了
                return redirect('myauth:login')
        else:
            changePwdForm = PasswordChangeForm(user=request.user)
        context = {}
        context["changePwdForm"] = changePwdForm
        # context["user"] = request.user
        return render(request,'myauth/changePassword.html',context)
    

    进入修改密码界面:



    查看源代码,复制form表单内容:
    修改changePassword.html,代码如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>修改个人密码</title>
    </head>
    <body>
    <h1>修改密码</h1>
    <a href="{% url 'myauth:index' %}">主页</a>
    <a href="{% url 'myauth:editorProfile' %}">修改个人信息</a>
    <a href="{% url 'myauth:userCenter' %}">个人中心</a>
    <a href="{% url 'myauth:logout' %}">注销</a>
    <form action="" method="post">
    {% csrf_token %}
        {{ changePwdForm.errors }}
    <p>
        <label for="id_old_password">旧密码:</label>
        <input type="password" name="old_password" autofocus required id="id_old_password">
    </p>
    <p>
        <label for="id_new_password1">新密码:</label>
        <input type="password" name="new_password1" required id="id_new_password1">
        <span class="helptext">
            <ul>
                <li>你的密码不能与其他个人信息太相似。</li>
                <li>你的密码必须包含至少 8 个字符。</li>
                <li>你的密码不能是大家都爱用的常见密码。</li>
                <li>你的密码不能全部为数字。</li>
            </ul>
        </span>
    </p>
    <p>
        <label for="id_new_password2">新密码确认:</label>
        <input type="password" name="new_password2" required id="id_new_password2">
    </p>
    <button type="submit">提交</button>
    </form>
    </body>
    </html>
    
    报错信息

    修改正确跳转到登录界面:


    验证码

    pip install django-simple-captcha
    settings.py注册app
    "captcha",

    python manage.py migrate
    #Operations to perform:
    #  Apply all migrations: admin, auth, captcha, contenttypes, myauth, sessions
    #Running migrations:
    #  Applying captcha.0001_initial... OK
    

    添加到auth 下的urls.py里面

    path('captcha/',include('captcha.urls')),
    

    打开forms.py,在自定义注册表单(DIYUserCreationForm)下添加如下代码:

    from captcha.fields import CaptchaField
    captcha=CaptchaField()
    

    打开register.html,在<div>{{ registerForm.errors.birthday}}</div>后面添加如下代码:

    {{ registerForm.captcha }}
    

    打开注册页面



    打开forms.py,添加引用,

    from django.contrib.auth.forms import UserCreationForm,UserChangeForm,AuthenticationForm
    class DIYLoginForm(AuthenticationForm):
        captcha=CaptchaField()
    

    打开myauth app下views.py里面的mylogin函数,添加如下代码;

    from .forms import DIYUserCreationForm,DIYUserChangeForm,DIYLoginForm
    def mylogin(request):
        if request.method =="POST":
            loginForm = DIYLoginForm(data=request.POST)
            if loginForm.is_valid():
                # username=request.POST.get('username')
                # password=request.POST.get('password')
                username=loginForm.cleaned_data['username']
                password=loginForm.cleaned_data['password']
                user=authenticate(request,username=username,password=password)
            # if user is None:
            #     return render(request,'myauth/login.html',{'error':"用户名不存在"})
            # else:
                login(request,user)#request,
                return redirect('myauth:index')
        else:
            loginForm=DIYLoginForm
            context={}
            context['loginForm']=loginForm()
            context['user']=request.user
            return render(request,'myauth/login.html',context)
    

    修改login.html里面的form标签添加一行代码:

    {{ loginForm.captcha }}
    {{ loginForm.errors.captcha }}
    

    打开登录页面


    相关文章

      网友评论

          本文标题:django2登录注册功能学习笔记

          本文链接:https://www.haomeiwen.com/subject/yqbieqtx.html