系统自带登陆注册(自定义)

Django系统自带的models模型,不需要手动创建,只需要迁移
python manage.py migrate


4.44.11.png

如果想要添加字段,需要继承AbstractUser
注意:
可以在原基础上添加属性,但不可以删除原有属性


setting.py 配置

# 将模型设置为自己创建的User类
AUTH_USER_MODEL = 'App.User'
# 指定验证用户的类
AUTHENTICATION_BACKEND = (
    'App.auth.MyBackend',
)

class User(AbstracutUser):
      iphone = models.CharField(max_length=11,null=True)
      icon = models.CharField(max_length=70,default='default.jpg')
生成迁移文件
python manage.py makemigrations
python manage.py migrate
查看表
5.31.01.png

多了两个字段 iphone和icon
如果注册时不想用手机号码,定义模型时
iphone字段null=True就可以了


接下来 在App下新建auth.py。
在auth.py中导入:
作用:重写authenticate认证用户的方法,

from django.contrib.auth.backends import ModelBackend
from App.models import User

class MyBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        try:
            user = User.objects.get(username=username)
        except:
            try:
                user = User.objects.get(iphone=username)
            except:
                return
            # 添加邮箱登陆也是可以的
            # 必须确保用户名,手机号,邮箱是唯一的
        if user.check_password(password):
            return user
        else:
            return

model.py

from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.

class User(AbstractUser):
    iphone = models.CharField(max_length=11)
    icon = models.CharField(max_length=70,default='default.jpg')

main.py

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
# from django.contrib.auth.models import User
from App.models import User
from django.shortcuts import render,HttpResponse, redirect
from django.urls import reverse


def index(req):
    # print(req.user.is_authenticated)
    return render(req,'index.html')

#注册
def register(req):
    if req.method == 'GET':
        return render(req,'register.html')
    if req.method == 'POST':
        u = User.objects.create_user(req.POST.get('username'),
                                     req.POST.get('email'),
                                     req.POST.get('userpass'),
                                     iphone='123')
        u.save()
    return redirect(reverse('App:login'))


#登录
def Login(req):
    if req.method == 'GET':
        return render(req,'login.html')

    if req.method == 'POST':
        #authenticate用户认证的方法 如果用户名和密码正确 则返回对象  否则返回None
        u = authenticate(username=req.POST.get('username'),
                         password=req.POST.get('userpass'))
        # print(u.username)
        # print(u.email)
        # print(u.date_joined)
        # print(u)
        if u:
            if u.is_active:
                login(req,u)
                # return HttpResponse('登录称该并且该用户已经激活 你只需要保持当前用户的登录状态')
                return redirect(reverse('App:index'))

    return HttpResponse('登录失败')

#退出登录
def Logout(req):
    logout(req)
    return redirect(reverse('App:index'))

@login_required(login_url='/login/')
def test(req):
    # if not req.user.is_authenticated:
    #     return redirect(reverse('App:index'))

    return HttpResponse('我是登录以后才能访问的路由地址')

base.html




{% block head %}
    {% block meta %}
    
    
    
    
    {% endblock %}

    Bootstrap 101 Template
    
    {% block style %}
    {% endblock %}
    {% block link %}
    
    {% endblock %}
{% endblock %}


{% block nav %}


{% endblock %}
{% block content %}
    
{% block pagecontent %} boot_base {% endblock %}
{% endblock %} {% block script %} {% endblock %}

index.html

{% extends 'common/base.html' %}
{% block title %}
    首页
{% endblock %}
{% block pagecontent %}

首页

{% endblock %}

login.html

{% extends 'common/base.html' %}
{% block title %}
    登录
{% endblock %}
{% block pagecontent %}
    {% if info %}
        
    {% endif %}
    
{% csrf_token %}
用户名:

密码:

{% endblock %}

register.html

{% extends 'common/base.html' %}
{% block title %}
    注册
{% endblock %}
{% block pagecontent %}
    
{% csrf_token %}
用户名:

密码:

确认密码:

邮箱:

{% endblock %}

urls.py

from django.conf.urls import url
from App.views import *
urlpatterns = [
    url(r'^$', main.index,name='index'),
    url(r'^login/$', main.Login,name='login'),
    url(r'^logout/$', main.Logout,name='logout'),
    url(r'^register/$', main.register,name='register'),
    url(r'^test/$', main.test,name='test'),
]

image

你可能感兴趣的:(系统自带登陆注册(自定义))