色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Django商城項(xiàng)目注冊(cè)功能的實(shí)現(xiàn)

瀏覽:4日期:2024-09-04 17:23:06
目錄設(shè)計(jì)到的前端知識(shí)注冊(cè)業(yè)務(wù)實(shí)現(xiàn)前端注冊(cè)業(yè)務(wù)邏輯導(dǎo)入vue.js和ajax請(qǐng)求的js庫(kù)準(zhǔn)備register.js文件后端業(yè)務(wù)注冊(cè)邏輯設(shè)計(jì)到的前端知識(shí)

項(xiàng)目的前端頁(yè)面使用vue來(lái)實(shí)現(xiàn)局部刷新,通過(guò)數(shù)據(jù)的雙向綁定實(shí)現(xiàn)與用戶(hù)的交互,下面來(lái)看一下需求,在用戶(hù)輸入內(nèi)容后,前端需要做一些簡(jiǎn)單的規(guī)則校驗(yàn),我們希望在在用戶(hù)輸入后能夠?qū)崟r(shí)檢測(cè),如果有錯(cuò)誤能夠在輸入框的下方顯示出來(lái)。

<li> <label>用戶(hù)名:</label> <input type='text' name='username' id='user_name'> <span class='error_tip'>請(qǐng)輸入5-20個(gè)字符的用戶(hù)</span></li><li> <label>密碼:</label> <input type='password' name='password' id='pwd'> <span class='error_tip'>請(qǐng)輸入8-20位的密碼</span></li>

上面是一個(gè)用戶(hù)和密碼的輸入框,當(dāng)用戶(hù)輸入完用戶(hù)名以后,光標(biāo)離開(kāi)輸入框,能夠?qū)崟r(shí)的檢測(cè)輸入內(nèi)容的正確性,當(dāng)輸入有問(wèn)題的時(shí)候,在輸入框的下方顯示錯(cuò)誤信息。

v-model實(shí)現(xiàn)數(shù)據(jù)的雙向綁定,v-on進(jìn)行事件綁定,v-show是控制dom顯示與否,下面是加入vue后的部分代碼

<li> <label>用戶(hù)名:</label> <input type='text' name='username' v-model='username' @blur='check_username'> <span v-show='error_name'>[[error_name_message]]</span></li><li> <label>密碼:</label> <input type='password' name='password' v-model='password' @blur='check_password'> <span v-show='error_password'>請(qǐng)輸入8-20位的密碼</span></li>

用戶(hù)輸入的用戶(hù)名和username變量綁定,光標(biāo)消失觸發(fā)綁定時(shí)間check_username,通過(guò)v-show綁定到布爾值變量error_name,來(lái)控制是否顯示字符串變量error_name_message,其他的輸入框都類(lèi)似這種操作。

注冊(cè)業(yè)務(wù)實(shí)現(xiàn)前端注冊(cè)業(yè)務(wù)邏輯

注冊(cè)表單代碼:

<form method='post' > {{ csrf_input }} <ul><li> <label>用戶(hù)名:</label> <input type='text' name='username' v-model='username' @blur='check_username'> <span v-show='error_name'>[[error_name_message]]</span></li><li> <label>密碼:</label> <input type='password' name='password' v-model='password' @blur='check_password'> <span v-show='error_password'>請(qǐng)輸入8-20位的密碼</span></li><li> <label>確認(rèn)密碼:</label> <input type='password' v-model='password2' @blur='check_password2' name='password2' id='cpwd'> <span v-show='error_password2'>兩次輸入的密碼不一致</span></li><li> <label>手機(jī)號(hào):</label> <input type='text' v-model='mobile' @blur='check_mobile' name='mobile' id='phone'> <span v-show='error_mobile'>[[ error_mobile_message ]]</span></li><li> <label>圖形驗(yàn)證碼:</label> <input type='text' name='image_code' class='msg_input'> <img src='http://www.lshqa.cn/bcjs/{{ static(’images/pic_code.jpg’) }}' alt='圖形驗(yàn)證碼' class='pic_code'> <span class='error_tip'>請(qǐng)?zhí)顚?xiě)圖形驗(yàn)證碼</span></li><li> <label>短信驗(yàn)證碼:</label> <input type='text' name='sms_code' class='msg_input'> <a href='javascript:;' rel='external nofollow' class='get_msg_code'>獲取短信驗(yàn)證碼</a> <span class='error_tip'>請(qǐng)?zhí)顚?xiě)短信驗(yàn)證碼</span></li><li class='agreement'> <input type='checkbox' name='allow' v-model='allow' @change='check_allow'> <label>同意”商城用戶(hù)使用協(xié)議“</label> <span v-show='error_allow'>請(qǐng)勾選用戶(hù)協(xié)議</span></li><li class='reg_sub'> <input type='submit' value='注 冊(cè)' @change='on_submit'> {% if register_errmsg %}<span class='error_tip2'>{{ register_errmsg }}</span> {% endif %}</li> </ul></form>導(dǎo)入vue.js和ajax請(qǐng)求的js庫(kù)

<script type='text/javascript' src='http://www.lshqa.cn/bcjs/{{ static(’js/vue-2.5.16.js’) }}'></script><script type='text/javascript' src='http://www.lshqa.cn/bcjs/{{ static(’js/axios-0.18.0.min.js’) }}'></script>準(zhǔn)備register.js文件

register.js文件主要處理注冊(cè)頁(yè)面的交互事件,并且向服務(wù)端提交注冊(cè)表單請(qǐng)求

<script type='text/javascript' src='http://www.lshqa.cn/bcjs/{{ static(’js/register.js’) }}'></script>

下面是實(shí)現(xiàn)的前端校驗(yàn)邏輯以及表單提交邏輯

methods: { // 校驗(yàn)用戶(hù)名 check_username() {let re = /^[a-zA-Z0-9_-]{5,20}$/;if (re.test(this.username)) { this.error_name = false;} else { this.error_name_message = ’請(qǐng)輸入5-20個(gè)字符的用戶(hù)名’; this.error_name = true;} }, // 校驗(yàn)密碼 check_password() {let re = /^[0-9A-Za-z]{8,20}$/;this.error_password = !re.test(this.password); }, // 校驗(yàn)確認(rèn)密碼 check_password2() {if (this.password !== this.password2) { this.error_password2 = true;} else { this.error_password2 = false;} }, // 校驗(yàn)手機(jī)號(hào) check_mobile() {let re = /^1[3-9]d{9}$/;if (re.test(this.mobile)) { this.error_mobile = false;} else { this.error_mobile_message = ’您輸入的手機(jī)號(hào)格式不正確’; this.error_mobile = true;} }, // 校驗(yàn)是否勾選協(xié)議 check_allow() {this.error_allow = !this.allow; }, // 監(jiān)聽(tīng)表單提交事件 on_submit() {this.check_username();this.check_password();this.check_password2();this.check_mobile();this.check_allow();# 輸入字段中有一個(gè)不符合規(guī)則就禁止if (this.error_name === true || this.error_password === true || this.error_password2 === true || this.error_mobile === true || this.error_allow === true) { // 禁用表單的提交 window.event.returnValue = false;} },}后端業(yè)務(wù)注冊(cè)邏輯

在用戶(hù)輸完用戶(hù)名之后,我們往往希望能夠跟快的給出這個(gè)用戶(hù)名是否符合注冊(cè)需求,前面只是對(duì)用戶(hù)名的規(guī)則進(jìn)行了校驗(yàn),還想知道他是否已經(jīng)在系統(tǒng)注冊(cè)過(guò)了,不然當(dāng)用戶(hù)都輸完提交注冊(cè)再給出用戶(hù)名或者手機(jī)號(hào)已經(jīng)注冊(cè)過(guò),體驗(yàn)不是特別好。所以需要在光標(biāo)離開(kāi)用戶(hù)名輸入框的時(shí)候就請(qǐng)求服務(wù)端來(lái)判斷是否注冊(cè)過(guò)。

定義路由

path(’register/’, views.RegisterView.as_view(), name=’register’), # name添加命名空間path(’usernames/<str:username>’, views.UsernameCountView.as_view(), name='username'),re_path(r’mobiles/(?P<mobile>1[3-9]d{9})’, views.MobileCountView.as_view(), name=’mobile’)

編寫(xiě)視圖類(lèi)

class UsernameCountView(View): def get(self, request, username):'''查詢(xún)?cè)撚脩?hù)名是否在系統(tǒng)中存在:param request: 請(qǐng)求對(duì)像:param username: 前端傳遞的用戶(hù)名:return:'''count = User.objects.filter(username=username).count()return http.JsonResponse({’code’:1001, ’msg’:’用戶(hù)已存在’}) if count == 1 else http.JsonResponse({’code’: 1000, ’msg’: ’’})

這里沒(méi)有對(duì)響應(yīng)做統(tǒng)一處理封裝,后面專(zhuān)門(mén)介紹一下。

然后就是注冊(cè)視圖類(lèi)的編寫(xiě)了:

class RegisterView(View): '''用戶(hù)注冊(cè)視圖類(lèi)''' def get(self, request):’’’獲取注冊(cè)頁(yè)面’’’return render(request, ’register.html’) def post(self, request):''''''username = request.POST.get(’username’)password = request.POST.get(’password’)password2 = request.POST.get(’password2’)mobile = request.POST.get(’mobile’)allow = request.POST.get(’allow’)# 判斷參數(shù)是否齊全if not all([username, password, password2, mobile, allow]): return http.HttpResponseForbidden(’缺少必傳參數(shù)’)# 判斷用戶(hù)名是否是5-20個(gè)字符if not re.match(r’^[a-zA-Z0-9_-]{5,20}$’, username): return http.HttpResponseForbidden(’請(qǐng)輸入5-20個(gè)字符的用戶(hù)名’)# 判斷密碼是否是8-20個(gè)數(shù)字if not re.match(r’^[0-9A-Za-z]{8,20}$’, password): return http.HttpResponseForbidden(’請(qǐng)輸入8-20位的密碼’)# 判斷兩次密碼是否一致if password != password2: return http.HttpResponseForbidden(’兩次輸入的密碼不一致’)# 判斷手機(jī)號(hào)是否合法if not re.match(r’^1[3-9]d{9}$’, mobile): return http.HttpResponseForbidden(’請(qǐng)輸入正確的手機(jī)號(hào)碼’)# 判斷是否勾選用戶(hù)協(xié)議if allow != ’on’: return http.HttpResponseForbidden(’請(qǐng)勾選用戶(hù)協(xié)議’)try: user = User.objects.create_user(username=username, password=password, mobile=mobile)except DatabaseError as e: return render(request, ’register.html’, {’register_errmsg’: e.args})# 注冊(cè)成功保存會(huì)話(huà)login(request, user)return redirect(reverse(’contents:index’))

django提供的login方法,封裝了寫(xiě)入session的操作,幫助我們快速登入一個(gè)用戶(hù),并實(shí)現(xiàn)狀態(tài)保持,將通過(guò)認(rèn)證的用戶(hù)的唯一標(biāo)識(shí)信息(比如:用戶(hù)ID)寫(xiě)入到當(dāng)前瀏覽器的 cookie 和服務(wù)端的 session 中。

request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)request.session[BACKEND_SESSION_KEY] = backendrequest.session[HASH_SESSION_KEY] = session_auth_hash

session會(huì)存入redis,之前在工程創(chuàng)建時(shí)進(jìn)行session存儲(chǔ)的配置

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'SESSION_CACHE_ALIAS = 'session'

鏈接: https://pan.baidu.com/s/1dFliI6KkNubd4k_OTEpqDA 提取碼: h3dp

以上就是Django商城項(xiàng)目注冊(cè)功能的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Django 注冊(cè)功能的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 国产一国产一有一级毛片 | 美女三级在线 | 国产人做人爱免费视频 | 国产一级一片免费播放视频 | 欧美日韩精品在线播放 | 国产成人综合网在线播放 | 九九99香蕉在线视频免费 | 美女很黄很黄是免费的·无遮挡网站 | 亚洲第一综合网站 | 盗摄偷拍a在线观看 | 国产人成亚洲第一网站在线播放 | 国产成人美女福利在线观看 | 女人张开腿等男人桶免费视频 | 国产在线一二三区 | 久久久久久国产精品免费免 | 国产美女在线精品观看 | 高清在线一区二区 | 免费一级夫妻a | 免费看美女毛片 | 国内一级特黄女人精品片 | 国产日韩精品一区二区三区 | 免费视频观看在线www日本 | 成人在线中文字幕 | 国产成人精品综合久久久 | 中文字幕天堂久久精品 | 久久久全国免费视频 | 免费观看成年人视频 | 成人欧美一区二区三区视频xxx | 欧美中文一区 | 欧美激情自拍 | 一区二区三区在线免费观看视频 | 久久国产精品高清一区二区三区 | 国产精品视频一区二区猎奇 | 夜间福利在线观看 | 成年网站在线在免费播放 | 欧美亚洲国产成人综合在线 | 免费看 s色 | 免费看特黄特黄欧美大片 | 亚洲欧美精选 | 欧美三级成版人版在线观看 | 久久国产精品自线拍免费 |