您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)利用django框架怎么實(shí)現(xiàn)一個(gè)用戶(hù)注冊(cè)、登錄功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
1 用戶(hù)注冊(cè):
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 # 用戶(hù)注冊(cè) @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('用戶(hù)名不能為空') 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('確認(rèn)密碼不能為空') 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 用戶(hù)登錄:
@csrf_exempt def my_login(request): errors =[] account = None password = None if request.method == "POST": if not request.POST.get('account'): errors.append('用戶(hù)名不能為空') 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('用戶(hù)名錯(cuò)誤') else: errors.append('用戶(hù)名或密碼錯(cuò)誤') return render(request,'blog/login.html', {'errors': errors})
3 用戶(hù)退出:
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'), ]
注冊(cè) 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 >用戶(hù)名:</label> </td> <td> <input type = 'text' placeholder="輸入用戶(hù)名" name = 'account'> </td> </tr> <tr> <td> <label >密碼:</label> </td> <td> <input type = 'password' placeholder="輸入密碼" name = 'password'> </td> </tr> <tr> <td> <label >確認(rèn)密碼:</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 >用戶(hù)名:</label> </td> <td> <input type = 'text' placeholder="輸入用戶(hù)名" 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>
關(guān)于利用django框架怎么實(shí)現(xiàn)一個(gè)用戶(hù)注冊(cè)、登錄功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。