Django URL name詳解
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:主機域名、雅安服務器托管、營銷軟件、網(wǎng)站建設、沁縣網(wǎng)站維護、網(wǎng)站推廣。將Temlate中所有的修改為相對路徑url注冊 登錄 將所有的靜態(tài)文件修改為相對路徑staticfile(前提在setting中設置好STATIC_URL 和STATICFILES_DIRS才能找到路徑)
以后所有寫成相對路徑
#模板中添加聲明 {% load staticfiles %} #添加方法,如此以來,即使修改了static的路徑也不會修改,因為引用的是相對路徑 安裝Django-simple-captcha,用于生成驗證碼。
下載連接-github
文檔說明
Installation
1、Install django-simple-captcha via pip: pip install django-simple-captcha
2、Add captcha to the INSTALLED_APPS in your settings.py
3、Run python manage.py migrate
4、Add an entry to your urls.py:
urlpatterns += [ url(r\'^captcha/\', include(\'captcha.urls\')), ] 添加驗證嗎 在app.form中建立register_form表單,定義好注冊需要的字段(form可自動生成input框)【form實現(xiàn)了后臺與前端的交互】
class RegisterForm(forms.Form): Email = forms.EmailField(required=True) Password = forms.CharField(required=True,min_length=5) captcha = CaptchaField() 將form加入RegisterView視圖中,并將form數(shù)據(jù)傳入Template中
class RegisterView(View): def get(self,request): register_form = RegisterForm() return render(request,\'register.html\',{\'register_form\':register_form}) #將register_form數(shù)據(jù)傳遞給Template 通過Template將register_form傳入html中,生成驗證碼(form數(shù)據(jù)可通過Template自動生成input框)
class RegisterView(View): def get(self,request): register_form = RegisterForm() return render(request,\'register.html\',{\'register_form\':register_form}) #將register_form數(shù)據(jù)傳遞給Template def post(self, request): register_form = RegisterForm(request.POST) #將post上來的數(shù)據(jù)傳遞給RegisterForm if register_form.is_valid(): #上傳的數(shù)據(jù)符合form表要求,有效 user_name = request.POST.get(\'username\',\'\') if UserProfile.objects.filter(email=user_name): return render(request, \'register.html\', {\'register_form\':register_form,\'msg\': \'用戶已存在\'}) # 該邏輯用戶判斷用戶是否已注冊存在 pass_word = request.POST.get(\'password\',\'\') user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name #明文數(shù)據(jù)需要經(jīng)過加密后傳入數(shù)據(jù)庫,利用make_password方法加密 user_profile.password = make_password(pass_word) user_profile.save() else: return render(request, \'register.html\', {\'register_form\': register_form}) # 將register_form數(shù)據(jù)傳遞給Template 注意html中相同的、多余的input會影響文本輸出 將value傳回Template中
通過郵箱發(fā)送驗證鏈接進行注冊激活 Django 使用 QQ / 新浪郵箱發(fā)送郵件配置 創(chuàng)建utils文件夾,用于存放工具函數(shù)/類
from users.models import EmailVerifyCode from random import Random from django.core.mail import send_mail from GMOOC.settings import EMAIL_HOST,EMAIL_PORT,EMAIL_HOST_USER,EMAIL_HOST_PASSWORD,EMAIL_USE_TLS,EMAIL_FROM 用于生成隨機生成驗證嗎,長度可調(diào)
#隨機生成驗證嗎,長度可調(diào) def random_str(random_length=8): code = \'\' chars = \'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890\' random = Random() length = len(chars)-1 for i in random_length: code += chars[random.randint(0,length)] return code 用于生成隨機驗證碼和對應的郵箱并存入數(shù)據(jù)庫中,將驗證碼以鏈接形式發(fā)送至郵箱,點擊進行激活
def send_register_email(email, send_type=\'register\'): ###########################生成隨機驗證碼存入數(shù)據(jù)庫中########################### #繼承模型 email_record = EmailVerifyCode() code = random_str(16) #將隨機生成code存入數(shù)據(jù)庫中 email_record.code = code #將管理的郵箱傳入數(shù)據(jù)庫中 email_record.email = email #定義發(fā)送類型 email_record.send_type = send_type email_record.save() ###########################發(fā)送激活郵件########################### email_title = \'\' email_body = \'\' receive_email = email if send_type == \'register\': email_title = \'慕學在線激活連接\' email_body = \'請點擊下方連接,激活注冊:\'+\'http://127.0.0.1:8000/active/{0}\'.format(code) if send_type ==\'find_password\': email_title = \'慕學在線密碼找回連接\' email_body = \'請點擊下方連接,進行密碼找回:\' + \'http://127.0.0.1:8000/active/{0}\'.format(code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [receive_email]) 將注冊頁上傳上來的信息保存值UserProfile,但是is_active還是0,表示還未進行激活。完成注冊后跳轉(zhuǎn)至登陸頁面,注冊失敗的仍停留在注冊頁
class RegisterView(View): def get(self,request): register_form = RegisterForm() return render(request,\'register.html\',{\'register_form\':register_form}) #將register_form數(shù)據(jù)傳遞給Template #return render(request,\'register.html\',{}) def post(self, request): register_form = RegisterForm(request.POST) #將post上來的數(shù)據(jù)傳遞給RegisterForm if register_form.is_valid(): #上傳的數(shù)據(jù)符合form表要求,有效 user_name = request.POST.get(\'email\',\'\') pass_word = request.POST.get(\'password\',\'\') ########注冊的時候需要查看郵箱是否有重復,利用了username進行了去重####### user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name #明文數(shù)據(jù)需要經(jīng)過加密后傳入數(shù)據(jù)庫,利用make_password方法加密 user_profile.password = make_password(pass_word) user_profile.is_active = 0 #表面用戶還未激活 user_profile.save() #用于郵件激活操作 send_register_email(user_name, send_type=\'register\') return render(request, \'login.html\', {}) # 將register_form數(shù)據(jù)傳遞給Template else: #注冊失敗跳轉(zhuǎn)至index頁面,暫時 #return render(request,\'index.html\',{}) return render(request, \'register.html\', {\'register_form\':register_form}) # 將register_form數(shù)據(jù)傳遞給Template 在url中,通過(?P.*)正則獲取獲取匹配的字符串(實際就是發(fā)送的驗證碼),綁定ActiveUserView,通過該類完成激活
url(r\'^active/(?P
class ActiveUserView(View): def get(self,request,active_code): #利用code在EmailVerifyCode進行查詢 EmailVerifyCodeRecorder = EmailVerifyCode.objects.filter(code=active_code) if EmailVerifyCodeRecorder: #找到UserProfile中的對應賬號,并設is_active為1,即為激活賬號 for i in EmailVerifyCodeRecorder: email = i.email user = UserProfile.objects.get(email=email) user.is_active = 1 user.save() return render(request, \'login.html\', {}) # 將register_form數(shù)據(jù)傳遞給Template else: return render(request, \'active_fail.html\', {}) #找不到記錄則返回連接失效的頁面 密碼找回 在Template中設置忘記密碼的跳轉(zhuǎn)
忘記密碼? 在url中定義forgetpwd頁面
url(r\'^forgetpwd/$\', ForgetpwdView.as_view(), name=\'forgetpwd\'), 定義ForgetpwdForm
class ForgetpwdForm(forms.Form): email = forms.EmailField(required=True) captcha = CaptchaField(error_messages={\'invalid\':u\'驗證碼錯誤\'}) 將form的驗證碼form傳入html中,并配置好靜態(tài)文件
#html中配置驗證碼 {{ forgetpwd_form.captcha }} 發(fā)送郵箱的函數(shù)
def send_email(email, send_type=\'register\'): ###########################生成隨機驗證碼存入數(shù)據(jù)庫中########################### #繼承模型 email_record = EmailVerifyCode() code = random_str(16) #將隨機生成code存入數(shù)據(jù)庫中 email_record.code = code #將管理的郵箱傳入數(shù)據(jù)庫中 email_record.email = email #定義發(fā)送類型 email_record.send_type = send_type email_record.save() ###########################發(fā)送激活郵件########################### email_title = \'\' email_body = \'\' receive_email = email if send_type == \'register\': email_title = \'慕學在線激活連接\' email_body = \'請點擊下方連接,激活注冊:\'+\'http://127.0.0.1:8000/active/{0}\'.format(code) if send_type ==\'find_password\': email_title = \'慕學在線密碼找回連接\' email_body = \'請點擊下方連接,進行密碼找回:\' + \'http://127.0.0.1:8000/password_reset/{0}\'.format(code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [receive_email]) 在view中定義ForgetpwdView后臺邏輯
class ForgetpwdView(View): def get(self,request): forgetpwd_form = ForgetpwdForm() return render(request,\'forgetpwd.html\',{\'forgetpwd_form\':forgetpwd_form}) #將驗證碼傳遞給前端 def post(self,request): forgetpwd_form = ForgetpwdForm(request.POST) if forgetpwd_form.is_valid(): #post數(shù)據(jù)成功,將驗證碼保存在EmailVerifyCode,用于激活查詢,并跳轉(zhuǎn)至index頁面 email = request.POST.get(\'email\') send_email(email, send_type=\'find_password\') return render(request,\'index.html\',{}) else: #驗證碼錯誤或郵箱格式錯誤返回信息 return render(request,\'forgetpwd.html\',{\'forgetpwd_form\':forgetpwd_form}) 注意form表單的變量名稱要與html中的name保持一直, forgetpwd_form = ForgetpwdForm(request.POST)才能匹配 編寫修改密碼連接的url,綁定PasswordReset
url(r\'^password_reset/(?P
class PasswordReset(View): def get(self,request,active_code): return render(request,\'password_reset.html\',{}) #獲取修改密碼頁面 def post(self,request,active_code): EmailVerifyCodeRecorder = EmailVerifyCode.objects.filter(code=active_code) #通過active_code去EmailVerifyCode找到對應的email if EmailVerifyCodeRecorder: #如果該驗證碼存在 # 找到UserProfile中的對應賬號,進行修改密碼 for i in EmailVerifyCodeRecorder: #一般來說該驗證碼是唯一的 email = i.email #找到email user = UserProfile.objects.get(email=email) #通過email在UserProfile中找到對應的用戶數(shù)據(jù) password_reset = PasswordResetForm(request.POST) #新密碼上傳至表單中 if password_reset.is_valid(): #新設置的密碼符合表單的話 password = request.POST.get(\'password\',\'\') password2 = request.POST.get(\'password2\',\'\') if password == password2: #兩個密碼相同,所有邏輯正確,進行密碼修改 #在數(shù)據(jù)庫中信息密碼修改 user.password = make_password(password) user.save() return render(request, \'login.html\', {}) # 將register_form數(shù)據(jù)傳遞給Template else: #如果兩個密碼不相同 return render(request, \'password_reset.html\', {\'msg\':\'兩個密碼不一致\'}) # 將register_form數(shù)據(jù)傳遞給Template else: #表單驗證不通過 return render(request, \'password_reset.html\', {\'msg\':\'密碼未填寫或格式有問題\'}) else: pass #返回404頁面 Template的修改
1.靜態(tài)文件配置:{% static \'name\' %} 2.url的配置:{% url \'name\' %} 3.錯誤信息提示:
用戶退出
前端配置“注銷按鈕”的觸發(fā)邏輯:單機“退出”跳轉(zhuǎn)至user_loginout的url中退出 在url中定義好視圖邏輯
url(r\'^loginout/$\', logout_view, name=\'user_loginout\'), 在view中定義好logout_view函數(shù)的邏輯,利用系統(tǒng)logout函數(shù)清除request的登陸信息
from django.contrib.auth import authenticate,login,logout def logout_view(request): logout(request) return render(request, \'index.html\', {}) # 登陸成功,由后臺渲染跳轉(zhuǎn)至index,并在index中判斷,頭部顯示