国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

django 框架實現的用戶注冊、登錄、退出功能示例

2024-07-09 22:42:08
字體:
來源:轉載
供稿:網友

本文實例講述了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 >        {{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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西畴县| 聂拉木县| 重庆市| 贡山| 德安县| 琼海市| 香港| 拜泉县| 阿荣旗| 芦溪县| 东方市| 南京市| 土默特左旗| 武夷山市| 安福县| 登封市| 崇义县| 曲松县| 巴马| 彭山县| 嵊泗县| 阿拉善右旗| 昆山市| 安义县| 丰顺县| 大名县| 九江市| 黄梅县| 太谷县| 蓬莱市| 山东省| 绍兴市| 土默特左旗| 江阴市| 盱眙县| 郸城县| 玛沁县| 郯城县| 碌曲县| 老河口市| 拜泉县|