django 框架實現(xiàn)的用戶注冊、登錄、退出功能示例
本文實例講述了django 框架實現(xiàn)的用戶注冊、登錄、退出功能。分享給大家供大家參考,具體如下:
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('確認(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('用戶名錯誤')
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 >確認(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>
希望本文所述對大家基于Django框架的Python程序設(shè)計有所幫助。
相關(guān)文章
Python通過屬性手段實現(xiàn)只允許調(diào)用一次的示例講解
下面小編就為大家分享一篇Python通過屬性手段實現(xiàn)只允許調(diào)用一次的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python中shape[0]、shape[1]和shape[-1]分別的意思詳解(附代碼)
剛開始使用python做東西,總是不太理解矩陣、數(shù)組相關(guān)的問題,所以在此記錄shape方面的總結(jié),下面這篇文章主要給大家介紹了關(guān)于Python中shape[0]、shape[1]和shape[-1]分別是什么意思的相關(guān)資料,需要的朋友可以參考下2022-11-11
Python pandas遍歷行數(shù)據(jù)的2種方法小結(jié)
pandas在數(shù)據(jù)處理過程中,除了對整列字段進(jìn)行處理之外,有時還需求對每一行進(jìn)行遍歷,本文就來介紹Python pandas遍歷行數(shù)據(jù)的2種方法小結(jié),感興趣的可以了解一下2024-03-03
Python+Django實現(xiàn)簡單HelloWord網(wǎng)頁的示例代碼
本文主要介紹了Python+Django實現(xiàn)簡單HelloWord網(wǎng)頁的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Python2與Python3的區(qū)別點(diǎn)整理
在本篇文章里小編給大家整理的是關(guān)于Python2與Python3的區(qū)別點(diǎn)整理內(nèi)容,需要的朋友們可以參考下。2019-12-12
Python threading Local()函數(shù)用法案例詳解
這篇文章主要介紹了Python threading Local()函數(shù)用法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Python從入門到實戰(zhàn)之?dāng)?shù)據(jù)結(jié)構(gòu)篇
數(shù)據(jù)結(jié)構(gòu)中有很多樹的結(jié)構(gòu),其中包括二叉樹、二叉搜索樹、2-3樹、紅黑樹等等。本文中對數(shù)據(jù)結(jié)構(gòu)進(jìn)行了總結(jié),不求嚴(yán)格精準(zhǔn),但求簡單易懂2021-11-11
Python面向?qū)ο笾畠?nèi)置函數(shù)相關(guān)知識總結(jié)
本次要總結(jié)的的內(nèi)置函數(shù)共8個,他們都跟面向?qū)ο蟮闹R相關(guān),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Python語言描述機(jī)器學(xué)習(xí)之Logistic回歸算法
這篇文章主要介紹了Python語言描述機(jī)器學(xué)習(xí)之Logistic回歸算法,涉及Sigmoid函數(shù),梯度上升法等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12

