您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关利用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<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 > {{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 > {{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框架怎么实现一个用户注册、登录功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。