Django的注册与登录

界面图:
Django的注册与登录_第1张图片
Django的注册与登录_第2张图片

对于用户的注册与登录,主要使用表单。
验证码实现调用包来完成。

1.现在models.py中建立数据表
Django的注册与登录_第3张图片
圈起来的意思是与User(Auth_User)一一对应的关系,后面得on_delete的意思是限制此表与User表同删,同增。
Django的注册与登录_第4张图片

2.在该App下新建Forms.py,
Django的注册与登录_第5张图片

3.Views.py中导入models.py与forms.py的相关东西
在这里插入图片描述
导入login,logout

// An highlighted block
//登录
def DLogin(request):
    if request.method == 'POST':
        print("POST")
        Login_Forms = DLoginForms(data=request.POST)
        print(Login_Forms)
        UserName = request.POST['username']
        print(UserName)
        if Login_Forms.is_valid(): #判断数据是否合法
            用户 = authenticate(request, username=Login_Forms.cleaned_data['username'],
                              password=Login_Forms.cleaned_data['password'])
            login(request, 用户)
            return redirect('DengLu:主页')
            content = {'Login_Forms': Login_Forms, '用户': request.user, }
        else:
            content = {'Login_Forms': Login_Forms, '用户': request.user, '用户名': UserName}

    else:
        print("GET")
        BackIMG = BackPhoto.objects.get(No=1)
        print(BackIMG)
        S1_IMG = BackIMG.PPhoto  # ImageFieldFile     OpenCV格式: False
        print('从数据库中读取的图片是Opencv格式:', isinstance(S1_IMG, np.ndarray))  # 判断图像数据是否是OpenCV格式: False
        S3_Img = Image.open(S1_IMG)

        Login_Forms = DLoginForms()
        content = {'Login_Forms': Login_Forms, '用户': request.user, }
    return render(request, 'DengLu/DLogin.html', content)
    
//注册
def DRegister(request):
    if request.method == 'POST':
        print("POST")
        Register_Form = DRegisterForms(request.POST)
        print(request.POST)
        # print(Register_Form)
        shuzi = request.POST['username']

        if shuzi.isdigit() == True:
            if Register_Form.is_valid():
                print('执行!!')
                Register_Form.save()
                DUser = authenticate(username=Register_Form.cleaned_data['username'],
                                     password=Register_Form.cleaned_data['password1'])
                DoctorTable_1(DUser=DUser,
                              DName=Register_Form.cleaned_data['DName'],
                              DDepartment=Register_Form.cleaned_data['DDepartment'],
                              DTitle=Register_Form.cleaned_data['DTitle'],
                              DHospital=Register_Form.cleaned_data['DHospital'],
                              DPhone=Register_Form.cleaned_data['DPhone'],
                              # DSex=request.POST.get('DSex'),
                              DSex=Register_Form.cleaned_data['DSex'],
                              ).save()
                # login(request, DUser)
                return redirect("DengLu:登录")
            else:
                print("空执行")
            content = {'Register_Form': Register_Form}
            return render(request, 'DengLu/DRegister.html', content)
        else:
            Register_Form = DRegisterForms()
            content = {'Register_Form': Register_Form}
            return render(request, 'DengLu/DRegister.html', content)
    else:
        print("Get")
        Register_Form = DRegisterForms()
        content = {'Register_Form': Register_Form}
        return render(request, 'DengLu/DRegister.html', content)

Html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title style="text-align: center">医生登录页面</title>

    <link rel="canonical" href="https://v4.bootcss.com/docs/examples/checkout/">
    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
          integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <!-- Favicons -->
    <!-- Custom styles for this template -->
</head>
<body class="text-center" background="">
<div class="center-in-center">

    <img class="mb-4" src="../../../.././media/Icons/Doctor.jpg" alt="" width="100" height="100">

    <h1 class="h3 mb-3 font-weight-normal">医生登录</h1>
    <form action="" method="post" class="form-signin">
        {% csrf_token %}
        <label class="sr-only">用户名</label>
        <input type="text" name="username" placeholder="用户名" class="form-control" value="{{ 用户名 }}">
        <br>
        <label class="sr-only">密码</label>
        <input type="password" name="password" placeholder="密码" class="form-control">
        <br>
        <label>验证码</label>
        {{ Login_Forms.DVerificationCode }}
        {{ Login_Forms.errors.DVerificationCode }}

        <br>
        <br>
        <button type="submit" class="btn btn-primary btn-lg btn-block">登录</button>

    </form>
    <a href="{% url 'DengLu:主页' %}">回到主页</a>
</div>

</body>
</html>

你可能感兴趣的:(Django)