Django小白教程之Django用戶注冊(cè)與登錄
Django 是由 Python 開發(fā)的一個(gè)免費(fèi)的開源網(wǎng)站框架,可以用于快速搭建高性能,優(yōu)雅的網(wǎng)站!
學(xué)習(xí)django學(xué)得超級(jí)吃力,最近弄個(gè)最簡(jiǎn)單的用戶登錄與注冊(cè)界面都是那么難,目前算是基本實(shí)現(xiàn)了,雖然功能特別特別簡(jiǎn)單但是做一個(gè)記錄,以后學(xué)習(xí)深入了再來補(bǔ)充:
首先創(chuàng)建項(xiàng)目,到項(xiàng)目所在目錄:django-admin startproject demo0414_userauth
進(jìn)入項(xiàng)目:cd demo0414_userauth
創(chuàng)建相應(yīng)的app:django-admin startapp account
整個(gè)項(xiàng)目的結(jié)構(gòu)圖如圖所示
├── account
│ ├── admin.py
│ ├── admin.pyc
│ ├── apps.py
│ ├── init.py
│ ├── init.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── init.py
│ │ └── init.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── demo0414_userauth
│ ├── init.py
│ ├── init.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── manage.py
└── templates
├── register.html
├── success.html
└── userlogin.html
4 directories, 29 files
然后在setting文件的installed_app中添加app account;
創(chuàng)建一個(gè)templates文件夾,可以放在項(xiàng)目的根目錄下也可以放在app的目錄下。一般情況下提倡放在app的目錄下。如果放下項(xiàng)目的根目錄下需要在setting文件中TEMPLATES中設(shè)置'DIRS': [os.path.join(BASE_DIR,'templates')],否則不能使用模板。
另外因?yàn)檫@個(gè)項(xiàng)目存在頁面跳轉(zhuǎn)的問題,為了安全防止csrf攻擊,一把模板中都有了相關(guān)的設(shè)置。目前我還不會(huì)用這個(gè)東西,據(jù)說在form表單中添加標(biāo)簽{% csrf_token %}就可以實(shí)現(xiàn)了,但是我沒有成功。所以先不考慮這個(gè)問題,把seeting中的這個(gè)中間件'django.middleware.csrf.CsrfViewMiddleware',注釋掉
然后在model中創(chuàng)建相應(yīng)的數(shù)據(jù)庫:
class User(models.Model): username = models.CharField(max_length=50) password = models.CharField(max_length=50) email = models.EmailField()
view中添加相應(yīng)的程序。Pdb當(dāng)時(shí)用于斷點(diǎn)調(diào)試,我很喜歡,超級(jí)喜歡。如果你不敢興趣,直接注釋即可。
#coding=utf-8 from django.shortcuts import render,render_to_response from django import forms from django.http import HttpResponse,HttpResponseRedirect from django.template import RequestContext from django.contrib import auth from models import User import pdb def login(request): if request.method == "POST": uf = UserFormLogin(request.POST) if uf.is_valid(): #獲取表單信息 username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] userResult = User.objects.filter(username=username,password=password) #pdb.set_trace() if (len(userResult)>0): return render_to_response('success.html',{'operation':"登錄"}) else: return HttpResponse("該用戶不存在") else: uf = UserFormLogin() return render_to_response("userlogin.html",{'uf':uf}) def register(request): curtime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()); if request.method == "POST": uf = UserForm(request.POST) if uf.is_valid(): #獲取表單信息 username = uf.cleaned_data['username'] #pdb.set_trace() #try: filterResult = User.objects.filter(username = username) if len(filterResult)>0: return render_to_response('register.html',{"errors":"用戶名已存在"}) else: password1 = uf.cleaned_data['password1'] password2 = uf.cleaned_data['password2'] errors = [] if (password2 != password1): errors.append("兩次輸入的密碼不一致!") return render_to_response('register.html',{'errors':errors}) #return HttpResponse('兩次輸入的密碼不一致!,請(qǐng)重新輸入密碼') password = password2 email = uf.cleaned_data['email'] #將表單寫入數(shù)據(jù)庫 user = User.objects.create(username=username,password=password1) #user = User(username=username,password=password,email=email) user.save() pdb.set_trace() #返回注冊(cè)成功頁面 return render_to_response('success.html',{'username':username,'operation':"注冊(cè)"}) else: uf = UserForm() return render_to_response('register.html',{'uf':uf}) class UserForm(forms.Form): username = forms.CharField(label='用戶名',max_length=100) password1 = forms.CharField(label='密碼',widget=forms.PasswordInput()) password2 = forms.CharField(label='確認(rèn)密碼',widget=forms.PasswordInput()) email = forms.EmailField(label='電子郵件') class UserFormLogin(forms.Form): username = forms.CharField(label='用戶名',max_length=100) password = forms.CharField(label='密碼',widget=forms.PasswordInput())
Tempaltes文件夾下總共有3個(gè)頁面:
Register.html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>用戶注冊(cè)</title> </head> <style type="text/css"> body{color:#efd;background:#453;padding:0 5em;margin:0} h1{padding:2em 1em;background:#675} h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em} p{margin:1em 0} </style> <body> <h1>注冊(cè)頁面:</h1> <form method = 'post' enctype="multipart/form-data"> {{uf.as_p}} {{errors}} </br> <input type="submit" value = "ok" /> </form> </body> </html>
Userlogin.html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>用戶注冊(cè)</title> </head> <style type="text/css"> body{color:#efd;background:#453;padding:0 5em;margin:0} h1{padding:2em 1em;background:#675} h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em} p{margin:1em 0} </style> <body> <h1>登錄頁面:</h1> <form method = 'post' enctype="multipart/form-data"> {{uf.as_p}} <input type="submit" value = "ok" /> </form> </body> </html>
Success.html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title></title> </head> <body> <form method = 'post'> <h1>恭喜,{{operation}}成功!</h1> </form> </body> </html>
更新數(shù)據(jù)庫:
運(yùn)行服務(wù)器:
注冊(cè)頁面:
如果注冊(cè)的用戶沒有注冊(cè)過,則能注冊(cè)成功點(diǎn)擊OK進(jìn)入success界面
登錄頁面:
點(diǎn)擊OK就能進(jìn)入到success頁面
關(guān)于Django用戶注冊(cè)與登錄教程就給大家介紹完了,希望對(duì)大家有所幫助!
- django用戶注冊(cè)、登錄、注銷和用戶擴(kuò)展的示例
- django的登錄注冊(cè)系統(tǒng)的示例代碼
- Django實(shí)現(xiàn)auth模塊下的登錄注冊(cè)與注銷功能
- django 框架實(shí)現(xiàn)的用戶注冊(cè)、登錄、退出功能示例
- 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和wxPython實(shí)現(xiàn)下載視頻封面
這篇文章主要為大家詳細(xì)介紹了如何使用Python和wxPython實(shí)現(xiàn)下載視頻封面,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04PyTorch梯度裁剪避免訓(xùn)練loss nan的操作
這篇文章主要介紹了PyTorch梯度裁剪避免訓(xùn)練loss nan的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05python創(chuàng)建與遍歷二叉樹的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于python創(chuàng)建與遍歷二叉樹的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03