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+Canvas實(shí)現(xiàn)財(cái)神爺接元寶小游戲
這篇文章主要介紹了VUE+Canvas實(shí)現(xiàn)財(cái)神爺接元寶小游戲,需要的朋友可以參考下本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-04-04
vue使用echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)排序效果
echarts在前端開發(fā)中實(shí)屬必不可缺的大數(shù)據(jù)可視化工具,這篇文章主要為大家詳細(xì)介紹了vue如何使用echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)排序效果,感興趣的可以了解下2023-10-10
Vue檢測(cè)屏幕變化來改變不同的charts樣式實(shí)例
這篇文章主要介紹了Vue檢測(cè)屏幕變化來改變不同的charts樣式實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue原理Compile之optimize標(biāo)記靜態(tài)節(jié)點(diǎn)源碼示例
這篇文章主要為大家介紹了vue原理Compile之optimize標(biāo)記靜態(tài)節(jié)點(diǎn)源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue3.0+ts引入詳細(xì)步驟以及語法校驗(yàn)報(bào)錯(cuò)問題解決辦法
Vue?3.0是一個(gè)非常流行的JavaScript框架,不僅易于學(xué)習(xí)和使用,而且可以與許多UI框架集成,下面這篇文章主要給大家介紹了關(guān)于vue3.0+ts引入詳細(xì)步驟以及語法校驗(yàn)報(bào)錯(cuò)問題的解決辦法,需要的朋友可以參考下2024-01-01
在vant中使用時(shí)間選擇器和popup彈出層的操作
這篇文章主要介紹了在vant中使用時(shí)間選擇器和popup彈出層的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11

