Django 中的登录和身份验证

时间:2021-06-12 16:43:16

标签: python django django-models django-views

我正在尝试在 def login(request) 内的 views.py 中为来自我的模型类“Signup”的特定用户名和密码创建登录功能和登录身份验证。有人可以在我现有的代码中帮助我如何操作或共享任何资源来执行以下操作。

附言我是 Django 的新手,由于互联网上的信息无法解决我的问题而不知所措。

我正在为 models.pyviews.pyurls.pylogin.html 添加我的代码片段。

"models.py"

from django.db import models

# Create your models here.

class SignUp(models.Model):
    username= models.CharField(max_length=100)
    email=models.EmailField(max_length=254)
    password=models.CharField(max_length=20)
    address=models.CharField(max_length=250)

"views.py"

from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models import SignUp
from django.contrib.auth import login,authenticate,logout

# Create your views here.

def login(request):
    if request.method == 'POST':
        username= request.POST['username']
        password= request.POST['password']

        user = authenticate(username=username,password=password)
        html2 = "<html><body>No such user</body></html>"

        if user is not None:
            login(request,user)
            return redirect('')
        else:
            return HttpResponse(html2)
    else:

        return render(request,'login.html')

def signup(request):

    if request.method == 'POST':
        username= request.POST['username']
        email= request.POST['email']
        password1= request.POST['password1']
        password2= request.POST['password2']
        address= request.POST['address']
        html = "<html><body>Confirm Password and Password should be same </body></html>"
        html1= "<html><body>User Already present </body></html>"

        if password1 != password2:
            return HttpResponse(html)
        else:
            for instance in SignUp.objects.all():
                if (instance.username == username) or (instance.email==email):
                    return HttpResponse(html1)
            signup=SignUp(username=username,email=email,password=password1,address=address)
            signup.save()

            return redirect('login')

    else:

        return render(request,'signup.html')

"urls.py"

from django.urls import path
from . import views

urlpatterns = [
    path('',views.signup, name='signup'),
    path('signup/',views.signup,name='signup'),
    path('login/',views.login,name='login'),

]

“登录.html”

<!--
Author: Colorlib
Author URL: https://colorlib.com
License: Creative Commons Attribution 3.0 Unported
License URL: http://creativecommons.org/licenses/by/3.0/
-->
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<!-- Custom Theme files -->
<link href="{% static 'styles/styles.css' %}" rel="stylesheet" type="text/css" media="all" />
<!-- //Custom Theme files -->
<!-- web font -->
<link href="//fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i" rel="stylesheet">
<!-- //web font -->
</head>
<body>
    <!-- main -->
    <div class="main-w3layouts wrapper">
        <h1>LOGIN</h1>
        <div class="main-agileinfo">
            <div class="agileits-top">
                <form action="/login/" method="post">
                    {% csrf_token %}
                    <input class="text" type="text" name="username" placeholder="Username" required="">
                    <input class="text" type="password" name="password" placeholder="Password" required="">
        

                    <input type="submit" value="LOGIN">
                </form>
                <!-- <p>Don't have an Account? <a href="#"> Login Now!</a></p> -->
            </div>
        </div>
        <!-- copyright -->
        <!-- <div class="colorlibcopy-agile">
            <p>© 2018 Colorlib Signup Form. All rights reserved | Design by <a href="https://colorlib.com/" target="_blank">Colorlib</a></p>
        </div> -->
        <!-- //copyright -->
        <ul class="colorlib-bubbles">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <!-- //main -->
</body>
</html>

1 个答案:

答案 0 :(得分:0)

好吧,我同意 yuv,

你要做的基本上就是替换这个

class SignUp(models.Model):
    username= models.CharField(max_length=100)
    email=models.EmailField(max_length=254)
    password=models.CharField(max_length=20)
    address=models.CharField(max_length=250)

与:

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
 """
 add the required fields that you need apart from django's builtin 
 user model
 """
 address = models.Textfield(_("address"))

现在这可以帮助满足模型的要求,您将能够在用户名和电子邮件等字段上使用身份验证方法,您在注册期间存储的密码使用 set_password 函数来散列密码并将其存储在您的数据库中,这可以后记安全使用。