django 框架实现的用户注册、登录、退出功能示例
框架(架构)  /  管理员 发布于 5年前   116
本文实例讲述了django 框架实现的用户注册、登录、退出功能。分享给大家供大家参考,具体如下:
1 用户注册:
from django.contrib import authfrom django.contrib.auth.models import Userfrom django.views.decorators.csrf import csrf_exemptfrom django.http import HttpResponseRedirect# 用户注册@csrf_exemptdef 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_exemptdef 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<article_id>[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:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body>{% if errors %} <li> {% for error in errors %} <p style="color: red;"> {{error}} </p> {% endfor %} </li> {% endif %}<table> <form action="" method="post">{% csrf_token %} <tr> <td> <label >用户名:</label> </td> <td> <input type = 'text' placeholder="输入用户名" name = 'account'> </td> </tr> <tr> <td> <label >密码:</label> </td> <td> <input type = 'password' placeholder="输入密码" name = 'password'> </td> </tr> <tr> <td> <label >确认密码:</label> </td> <td> <input type = 'password' placeholder="再次输入密码" name ='password2'> </td> </tr> <tr> <td> <label>邮箱:</label> </td> <td> <input type="email" placeholder="输入邮箱" name = 'email'> </td> </tr> <tr> <td> <input type = 'submit' placeholder="Login" value="登录"> </td> </tr> </form></table></body></html>
登录HTML:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>登录</title></head><body>{% if errors %} <li> {% for error in errors %} <p style="color: red;"> {{error}} </p> {% endfor %} </li> {% endif %}<table> <form action="" method="post">{% csrf_token %} <tr> <td> <label >用户名:</label> </td> <td> <input type = 'text' placeholder="输入用户名" name = 'account'> </td> </tr> <tr> <td> <label >密码:</label> </td> <td> <input type = 'password' placeholder="输入密码" name = 'password'> </td> </tr> <tr> <td> <input type = 'submit' placeholder="Login" value="登录"> </td> </tr> </form></table></body></html></body></html>
希望本文所述对大家基于Django框架的Python程序设计有所帮助。
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号