Django小白教程之Django用戶注冊與登錄
Django 是由 Python 開發(fā)的一個免費的開源網(wǎng)站框架,可以用于快速搭建高性能,優(yōu)雅的網(wǎng)站!
學(xué)習(xí)django學(xué)得超級吃力,最近弄個最簡單的用戶登錄與注冊界面都是那么難,目前算是基本實現(xiàn)了,雖然功能特別特別簡單但是做一個記錄,以后學(xué)習(xí)深入了再來補(bǔ)充:
首先創(chuàng)建項目,到項目所在目錄:django-admin startproject demo0414_userauth
進(jìn)入項目:cd demo0414_userauth
創(chuàng)建相應(yīng)的app:django-admin startapp account
整個項目的結(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)建一個templates文件夾,可以放在項目的根目錄下也可以放在app的目錄下。一般情況下提倡放在app的目錄下。如果放下項目的根目錄下需要在setting文件中TEMPLATES中設(shè)置'DIRS': [os.path.join(BASE_DIR,'templates')],否則不能使用模板。
另外因為這個項目存在頁面跳轉(zhuǎn)的問題,為了安全防止csrf攻擊,一把模板中都有了相關(guān)的設(shè)置。目前我還不會用這個東西,據(jù)說在form表單中添加標(biāo)簽{% csrf_token %}就可以實現(xiàn)了,但是我沒有成功。所以先不考慮這個問題,把seeting中的這個中間件'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)時用于斷點調(diào)試,我很喜歡,超級喜歡。如果你不敢興趣,直接注釋即可。
#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('兩次輸入的密碼不一致!,請重新輸入密碼') 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() #返回注冊成功頁面 return render_to_response('success.html',{'username':username,'operation':"注冊"}) 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個頁面:
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>用戶注冊</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}} {{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>用戶注冊</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ù)器:
注冊頁面:
如果注冊的用戶沒有注冊過,則能注冊成功點擊OK進(jìn)入success界面
登錄頁面:
點擊OK就能進(jìn)入到success頁面
關(guān)于Django用戶注冊與登錄教程就給大家介紹完了,希望對大家有所幫助!
- django用戶注冊、登錄、注銷和用戶擴(kuò)展的示例
- django的登錄注冊系統(tǒng)的示例代碼
- Django實現(xiàn)auth模塊下的登錄注冊與注銷功能
- django 框架實現(xiàn)的用戶注冊、登錄、退出功能示例
- Django調(diào)用百度AI接口實現(xiàn)人臉注冊登錄代碼實例
- Django用戶登錄與注冊系統(tǒng)的實現(xiàn)示例
- django+vue實現(xiàn)注冊登錄的示例代碼
- Django 登錄注冊的實現(xiàn)示例
- Django制作簡易注冊登錄系統(tǒng)的實現(xiàn)示例
- django authentication 登錄注冊的實現(xiàn)示例
相關(guān)文章
使用Python和wxPython實現(xiàn)下載視頻封面
這篇文章主要為大家詳細(xì)介紹了如何使用Python和wxPython實現(xiàn)下載視頻封面,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04PyTorch梯度裁剪避免訓(xùn)練loss nan的操作
這篇文章主要介紹了PyTorch梯度裁剪避免訓(xùn)練loss nan的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05