django+vue實(shí)現(xiàn)注冊登錄的示例代碼
前臺利用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)!
相關(guān)文章:
1. ASP基礎(chǔ)知識VBScript基本元素講解2. Python requests庫參數(shù)提交的注意事項(xiàng)總結(jié)3. IntelliJ IDEA導(dǎo)入jar包的方法4. ajax請求添加自定義header參數(shù)代碼5. 使用Python和百度語音識別生成視頻字幕的實(shí)現(xiàn)6. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法7. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動庫8. 使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼9. vue-electron中修改表格內(nèi)容并修改樣式10. python操作mysql、excel、pdf的示例
