栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

django 框架实现的用户注册、登录、退出功能示例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

django 框架实现的用户注册、登录、退出功能示例

本文实例讲述了django 框架实现的用户注册、登录、退出功能。分享给大家供大家参考,具体如下:

1 用户注册:

from django.contrib import auth
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect
# 用户注册
@csrf_exempt
def register(request):
  errors = []
  account = None
  password = None
  password2 = None
  email = None
  CompareFlag = False
  if request.method == 'POST':
    if not request.POST.get('account'):
      errors.append('用户名不能为空')
    else:
      account = request.POST.get('account')
    if not request.POST.get('password'):
      errors.append('密码不能为空')
    else:
      password = request.POST.get('password')
    if not request.POST.get('password2'):
      errors.append('确认密码不能为空')
    else:
      password2 = request.POST.get('password2')
    if not request.POST.get('email'):
      errors.append('邮箱不能为空')
    else:
      email = request.POST.get('email')
    if password is not None:
      if password == password2:
 CompareFlag = True
      else:
 errors.append('两次输入密码不一致')
    if account is not None and password is not None and password2 is not None and email is not None and CompareFlag :
      user = User.objects.create_user(account,email,password)
      user.save()
      userlogin = auth.authenticate(username = account,password = password)
      auth.login(request,userlogin)
      return HttpResponseRedirect('/blog')
  return render(request,'blog/register.html', {'errors': errors})

2 用户登录:

@csrf_exempt
def my_login(request):
  errors =[]
  account = None
  password = None
  if request.method == "POST":
    if not request.POST.get('account'):
      errors.append('用户名不能为空')
    else:
      account = request.POST.get('account')
    if not request.POST.get('password'):
      errors = request.POST.get('密码不能为空')
    else:
      password = request.POST.get('password')
    if account is not None and password is not None:
      user = auth.authenticate(username=account,password=password)
      if user is not None:
 if user.is_active:
   auth.login(request,user)
   return HttpResponseRedirect('/blog')
 else:
   errors.append('用户名错误')
      else:
 errors.append('用户名或密码错误')
  return render(request,'blog/login.html', {'errors': errors})

3 用户退出:

def my_logout(request):
  auth.logout(request)
  return HttpResponseRedirect('/blog')

URL:

urlpatterns = [
  url(r'^$', views.index, name='index'),
  url(r'^p/(?P[0-9]+)/$', views.detail,name='detail'),
  url(r'^register/$',views.register, name='register'),
  url(r'^login/$',views.my_login, name='my_login'),
  url(r'^logout/$',views.my_logout, name='my_logout'),
]

注册 HTML:




  
  Title


{% if errors %}
    
  • {% for error in errors %}

    {{error}}

    {% endfor %}
  • {% endif %}

    登录HTML:

    
    
    
      
      Title
    
    
    
    
    
      
      登录
    
    
    {% if errors %}
        
  • {% for error in errors %}

    {{error}}

    {% endfor %}
  • {% endif %}

    希望本文所述对大家基于Django框架的Python程序设计有所帮助。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/23051.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号