国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

django+vue實(shí)現(xiàn)注冊登錄的示例代碼

瀏覽:2日期:2023-11-24 13:02:48
注冊

前臺利用vue中的axios進(jìn)行傳值,將獲取到的賬號密碼以form表單的形式發(fā)送給后臺。form表單的作用就是采集數(shù)據(jù),也就是在前臺頁面中獲取用戶輸入的值。numberValidateForm:前臺定義的表單$axios使用時(shí)需要在main.js中全局注冊,.then代表成功后進(jìn)行的操作,.catch代表失敗后進(jìn)行的操作

submitForm(formName) { let data = new FormData() data.append(’username’,this.numberValidateForm.name) data.append(’password’,this.numberValidateForm.pass) this.$axios.post(’/api/register/’,data).then((res) => {this.$router.push({ name: 'login' }) // 路由跳轉(zhuǎn) }).catch((res) => { console.log('error submit!!'); return false; }) }

使用$axios進(jìn)行跨域驗(yàn)證,首先得設(shè)置代理,然后在請求頭中加入X-CSRFToken

vue.config.js

代理

proxy: {'/api':{ target:'http://127.0.0.1:8000/', changeOrigin: true // 是否代理} },//設(shè)置代理,

main.js

import Axios from ’axios’Vue.prototype.$axios = Axioslet getCookie = function (cookie) { let reg = /csrftoken=([w]+)[;]?/g return reg.exec(cookie)[1]}Axios.interceptors.request.use( function(config) { // 在post請求前統(tǒng)一添加X-CSRFToken的header信息 let cookie = document.cookie; if(cookie && config.method == ’post’){ config.headers[’X-CSRFToken’] = getCookie(cookie); } return config; }, function(error) { // Do something with request error return Promise.reject(error); });登錄

submitForm(formName) { this.$refs[formName].validate(valid => { //vue前臺的驗(yàn)證規(guī)則if (valid) { let data = new FormData() data.append(’username’,this.numberValidateForm.name) data.append(’password’,this.numberValidateForm.pass) this.$axios.post(’/api/login/’,data).then((res) => { if(res.data.code == 'ok'){ console.log(12345678) this.$router.push({name:'firstpage'}) } })} else { console.log('error submit!!'); return false;} }); },

view.py

django后臺視圖函數(shù)

from django.shortcuts import renderfrom django.views import Viewfrom django.http import HttpResponse,JsonResponsefrom django.contrib.auth.models import User # django封裝好的驗(yàn)證功能from django.contrib import authclass Login(View): def post(self,request):try: user = request.POST.get(’username’,None) pwd = request.POST.get(’password’,None) # 驗(yàn)證密碼 obj = auth.authenticate(request,username=user,password=pwd) if obj:return JsonResponse({’code’:’ok’,’message’:’賬號密碼驗(yàn)證成功’})except: return JsonResponse({’code’:’no’,’message’:’驗(yàn)證失敗’})class Register(View): def post(self, request):try: username = request.POST.get(’username’,None) password = request.POST.get(’password’,None) user = User.objects.create_user(username=username,password=password) user.save()except: return JsonResponse({’code’:’no’,’message’:’注冊失敗’})return JsonResponse({’code’:’ok’,’message’:’注冊成功’})

到此這篇關(guān)于django+vue實(shí)現(xiàn)注冊登錄的示例代碼的文章就介紹到這了,更多相關(guān)django+vue注冊登錄內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 国产精品亚洲精品日韩已方 | 亚洲精品在线网站 | 一级视频在线观看 | 成人免费福利网站在线看 | 欧美在线观看不卡 | 国产欧美va欧美va香蕉在线 | 亚洲国产精品a一区二区三区 | 亚洲国产系列 | 亚洲精品www久久久久久久软件 | 国产成人综合亚洲亚洲欧美 | 国产一区二区三区四区五区 | 国产页| 在线国产日韩 | 成人午夜影院 | 久久aa毛片免费播放嗯啊 | 青青草原色 | 91欧美在线 | 最新亚洲人成网站在线影院 | 亚洲欧美精品网站在线观看 | 亚洲精品毛片久久久久久久 | 福利片免费一区二区三区 | 久久99亚洲网美利坚合众国 | 亚洲国产精品二区久久 | 欧美综合自拍亚洲综合百度 | 一级毛片在线免费播放 | 精品手机在线 | 亚洲国产精品看片在线观看 | 国产免费人视频在线观看免费 | 毛片免费看 | 1级a的观看视频 | 99在线视频精品 | 国产第一页在线观看 | 在线视频免费国产成人 | 国产一区精品在线观看 | 久在草在线 | 69国产成人综合久久精品91 | 国产成人精品一区二区三在线观看 | 国产91久久最新观看地址 | 国产老鸭窝毛片一区二区 | 久草网首页 | 成年女人免费观看视频 |