PythonDjango怎么实现登录注册-创新互联

本篇内容介绍了“Python Django怎么实现登录注册”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

成都创新互联专注骨干网络服务器租用十载,服务更有保障!服务器租用,成都移动云计算中心 成都服务器租用,成都服务器托管,骨干网络带宽,享受低延迟,高速访问。灵活、实现低成本的共享或公网数据中心高速带宽的专属高性能服务器。

1.modles层 与admin

点击(此处)折叠或打开

  1. # -*- coding: utf-8 -*-

  2. from __future__ import unicode_literals

  3. from django.db import models

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

  5. from django.utils.encoding import python_2_unicode_compatible

  6. ###普通用户的数据模型

  7. @python_2_unicode_compatible

  8. class NewUser(AbstractUser):

  9.     profile = models.CharField('profile', default='', max_length=256)

  10.     def __str__(self):

  11.         return self.username

  12. # Create your models here.



##admin
# -*- coding: utf-8 -*- 

from django.contrib import admin from .models import NewUser class NewUserAdmin(admin.ModelAdmin):
    list_display = ('username','date_joined', 'profile')



admin.site.register(NewUser,NewUserAdmin)

2.setting设置newuser管理系统账号进行验证

  1. AUTH_USER_MODEL = "pric.NewUser"

3  views视图层

  1. # -*- coding: utf-8 -*-

  2. from django.shortcuts import render

  3. from .forms import *

  4. from .models import *

  5. from django.contrib.auth import authenticate, login, logout

  6. from django.http import HttpResponse

  7. from django.shortcuts import render, redirect,render_to_response

  8. from django.core.exceptions import ObjectDoesNotExist

  9. from django.contrib.auth.decorators import login_required

  10. @login_required

  11. def index(request):

  12.     return render_to_response('index.html', locals())

  13. ###登录

  14. def log_in(request):

  15.     if request.method == 'GET':

  16.         form = LoginForm()

  17.         return render(request, 'login.html', {'form': form})

  18.     if request.method == 'POST': # 当提交表单时

  19.         form = LoginForm(request.POST) # form 包含提交的数据

  20.         if form.is_valid(): # 如果提交的数据合法

  21.             username = form.cleaned_data['uid']

  22.             password = form.cleaned_data['pwd']

  23.             user = authenticate(username=username, password=password)  

  24.             if user is not None: ###如果用户验证存在,

  25.                 login(request, user) #开始登录

  26.                 return redirect('index') #跳转首页

  27.             else:

  28.                 return render(request, 'login.html', {'form': form, 'msg': "password or username is not ture!"})

  29.         else: ###如果提交的数据不合法,继续登录

  30.             return render(request, 'login.html', {'form': form})

  31. ###登出

  32. @login_required

  33. def log_out(request):

  34.     logout(request)

  35.     return redirect('login')

  36. # Create your views here.

  37. ###注册

  38. def register(request):

  39.     error1 = "this name is already exist"

  40.     valid = "this name is valid"

  41.     if request.method == 'GET':

  42.         form = RegisterForm()

  43.         return render(request, 'register.html', {'form': form})

  44.     if request.method == 'POST': ###提交

  45.         form = RegisterForm(request.POST)

  46.         if form.is_valid():

  47.                 username = form.cleaned_data['username']

  48.                 email = form.cleaned_data['email']

  49.                 password1 = form.cleaned_data['password1']

  50.                 password2 = form.cleaned_data['password2']

  51.                 if password1 != password2:

  52.                     return render(request, 'register.html', {'form': form, 'msg': "two password is not equal"})

  53.                 else:

  54.                     NewUser.objects.create_user(username=username,password=password1,email=email)

  55.                     #user = NewUser(username=username, email=email, password=password1)

  56.                     #user.save()

  57.                     # return render(request, 'login.html', {'success': "you have successfully registered!"})

  58.                     return redirect('login')

  59.         else:

  60.                 return render(request, 'register.html', {'form': form})

4 首页获取当前登录用户

  1. 方式一:
    前端request.user.is_authenticated()判断当前用户是否登录 


  2.         

  3.           {% if request.user.is_authenticated %}

  4.             hihi: {{ request.user }}

  5.           登出

  6.           {% endif %}

  7.       



方式二 将request.user.is_authenticated判断写入views视图再传入html中

  1. @login_required

  2. def index(request):

  3.     if request.user.is_authenticated(): # 判断用户是否已登录

  4.         user = request.user;

  5.         return render_to_response('index.html', locals())

  6.  


  7.         

  8.              欢迎登录!{{ user }}

  9.           登出

  10.           

  11.       

“Python Django怎么实现登录注册”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联-成都网站建设公司网站,小编将为大家输出更多高质量的实用文章!


网页标题:PythonDjango怎么实现登录注册-创新互联
转载注明:http://cdiso.cn/article/dpehhh.html

其他资讯