django+vue實(shí)現(xiàn)注冊(cè)登錄的示例代碼
注冊(cè)
前臺(tái)利用vue中的axios進(jìn)行傳值,將獲取到的賬號(hào)密碼以form表單的形式發(fā)送給后臺(tái)。
form表單的作用就是采集數(shù)據(jù),也就是在前臺(tái)頁面中獲取用戶輸入的值。numberValidateForm:前臺(tái)定義的表單
$axios使用時(shí)需要在main.js中全局注冊(cè),.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è)置代理,然后在請(qǐng)求頭中加入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 = Axios
let getCookie = function (cookie) {
let reg = /csrftoken=([\w]+)[;]?/g
return reg.exec(cookie)[1]
}
Axios.interceptors.request.use(
function(config) {
// 在post請(qǐng)求前統(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前臺(tái)的驗(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后臺(tái)視圖函數(shù)
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse,JsonResponse
from django.contrib.auth.models import User # django封裝好的驗(yàn)證功能
from django.contrib import auth
class 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':'賬號(hào)密碼驗(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':'注冊(cè)失敗'})
return JsonResponse({'code':'ok','message':'注冊(cè)成功'})
到此這篇關(guān)于django+vue實(shí)現(xiàn)注冊(cè)登錄的示例代碼的文章就介紹到這了,更多相關(guān)django+vue注冊(cè)登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Django小白教程之Django用戶注冊(cè)與登錄
- django用戶注冊(cè)、登錄、注銷和用戶擴(kuò)展的示例
- django的登錄注冊(cè)系統(tǒng)的示例代碼
- Django實(shí)現(xiàn)auth模塊下的登錄注冊(cè)與注銷功能
- django 框架實(shí)現(xiàn)的用戶注冊(cè)、登錄、退出功能示例
- Django調(diào)用百度AI接口實(shí)現(xiàn)人臉注冊(cè)登錄代碼實(shí)例
- Django用戶登錄與注冊(cè)系統(tǒng)的實(shí)現(xiàn)示例
- Django 登錄注冊(cè)的實(shí)現(xiàn)示例
- Django制作簡(jiǎn)易注冊(cè)登錄系統(tǒng)的實(shí)現(xiàn)示例
- django authentication 登錄注冊(cè)的實(shí)現(xiàn)示例
相關(guān)文章
在vue項(xiàng)目中,將juery設(shè)置為全局變量的方法
今天小編就為大家分享一篇在vue項(xiàng)目中,將juery設(shè)置為全局變量的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue讀取本地靜態(tài)文件json的2種方法以及優(yōu)缺點(diǎn)
這篇文章主要介紹了Vue讀取本地靜態(tài)文件json的2種方法以及優(yōu)缺點(diǎn)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
詳解vue express啟動(dòng)數(shù)據(jù)服務(wù)
本篇文章主要介紹了vue express啟動(dòng)數(shù)據(jù)服務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
vue項(xiàng)目百度地圖如何自定義標(biāo)注marker
這篇文章主要介紹了vue項(xiàng)目百度地圖如何自定義標(biāo)注marker問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
關(guān)于axios配置請(qǐng)求頭content-type實(shí)例詳解
現(xiàn)在前端開發(fā)中需要通過Ajax發(fā)送請(qǐng)求獲取后端數(shù)據(jù)是很普遍的一件事情了,下面這篇文章主要介紹了關(guān)于axios配置請(qǐng)求頭content-type的相關(guān)資料,需要的朋友可以參考下2022-04-04
vue項(xiàng)目打包后網(wǎng)頁的title亂碼解決方案
這篇文章主要介紹了vue項(xiàng)目打包后網(wǎng)頁的title亂碼解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue工程全局設(shè)置ajax的等待動(dòng)效的方法
這篇文章主要介紹了vue工程全局設(shè)置ajax的等待動(dòng)效的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
Vue之Element級(jí)聯(lián)選擇器多選傳值以及回顯方式(樹形結(jié)構(gòu))
這篇文章主要介紹了Vue之Element級(jí)聯(lián)選擇器多選傳值以及回顯方式(樹形結(jié)構(gòu)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

