django 框架實(shí)現(xiàn)的用戶注冊(cè)、登錄、退出功能示例
本文實(shí)例講述了django 框架實(shí)現(xiàn)的用戶注冊(cè)、登錄、退出功能。分享給大家供大家參考,具體如下:
1 用戶注冊(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
# 用戶注冊(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('用戶名不能為空')
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 用戶登錄:
@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('用戶名錯(cuò)誤')
else:
errors.append('用戶名或密碼錯(cuò)誤')
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'), ]
注冊(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 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 >確認(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 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>
希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。
- Django小白教程之Django用戶注冊(cè)與登錄
- django用戶注冊(cè)、登錄、注銷(xiāo)和用戶擴(kuò)展的示例
- django的登錄注冊(cè)系統(tǒng)的示例代碼
- Django實(shí)現(xiàn)auth模塊下的登錄注冊(cè)與注銷(xiāo)功能
- Django調(diào)用百度AI接口實(shí)現(xiàn)人臉注冊(cè)登錄代碼實(shí)例
- Django用戶登錄與注冊(cè)系統(tǒng)的實(shí)現(xiàn)示例
- django+vue實(shí)現(xiàn)注冊(cè)登錄的示例代碼
- Django 登錄注冊(cè)的實(shí)現(xiàn)示例
- Django制作簡(jiǎn)易注冊(cè)登錄系統(tǒng)的實(shí)現(xiàn)示例
- django authentication 登錄注冊(cè)的實(shí)現(xiàn)示例
相關(guān)文章
python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(10):webpy框架
webpy小巧,簡(jiǎn)單,實(shí)用,可以快速的完成簡(jiǎn)單的web頁(yè)面。這里根據(jù)webpy Cookbook簡(jiǎn)要的介紹一下webpy框架,需要的朋友可以參考下2014-06-06
OpenCV根據(jù)面積篩選連通域?qū)W習(xí)示例
這篇文章主要為大家介紹了OpenCV根據(jù)面積篩選連通域?qū)W習(xí)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
python 如何將浮點(diǎn)數(shù)尾部無(wú)效0去掉和無(wú)效的‘.’號(hào)
這篇文章主要介紹了python 如何將浮點(diǎn)數(shù)尾部無(wú)效0去掉和無(wú)效的‘.’號(hào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
python實(shí)現(xiàn)網(wǎng)頁(yè)錄音效果
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)網(wǎng)頁(yè)錄音效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
python爬取數(shù)據(jù)中的headers和代理IP問(wèn)題分析
這篇文章主要為大家介紹了python爬取數(shù)據(jù)中的headers和代理IP問(wèn)題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Python驗(yàn)證碼識(shí)別處理實(shí)例
這篇文章主要介紹了Python驗(yàn)證碼識(shí)別處理實(shí)例,實(shí)現(xiàn)過(guò)程講解很詳細(xì),感興趣的小伙伴們可以參考一下2015-12-12
Flask框架web開(kāi)發(fā)之零基礎(chǔ)入門(mén)
這篇文章主要介紹了Flask框架web開(kāi)發(fā)之零基礎(chǔ)入門(mén),簡(jiǎn)單的介紹了如何使用及其數(shù)據(jù)庫(kù)集成,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Django-xadmin后臺(tái)導(dǎo)入json數(shù)據(jù)及后臺(tái)顯示信息圖標(biāo)和主題更改方式
這篇文章主要介紹了Django-xadmin后臺(tái)導(dǎo)入json數(shù)據(jù)及后臺(tái)顯示信息圖標(biāo)和主題更改方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

