import axios from 'axios';
axios.interceptors.request.use(
config => {
let ttoken = JSON.parse(localStorage.getItem('token'));
if (ttoken !== null) {
config.headers['Authorization'] = 'Token ' + ttoken;
}
return config;
}, function (error) {
return Promise.reject(error);
}
);
axios.defaults.withCredentials = true;
Vue.prototype.$ajax = axios;
<Alert v-show="isshow" type="error" show-icon closable>
提交錯誤
<span slot="desc">{{ e }} </span>
</Alert>
<script>
import Cookies from 'js-cookie';
export default {
data () {
return {
form: {
username: 'admin',
password: '1qaz.2wsx'
},
isshow: '',
e: '',
rules: {
username: [
{ required: true, message: '賬號不能為空', trigger: 'blur' }
],
password: [
{ required: true, message: '密碼不能為空', trigger: 'blur' }
]
}
};
},
methods: {
handleSubmit: function () {
this.$refs.loginForm.validate((valid) => {
if (valid) {
this.$ajax.post('http://127.0.0.1:8000/api-token-auth', this.form, {emulateJSON: true})
.then((res) => {
console.log(res);
if (res.statusText !== 'OK') {
this.isshow = true;
this.e = JSON.stringify(res.data.data);
} else {
Cookies.set('user', this.form.username);
localStorage.setItem('token', JSON.stringify(res.data.token));
this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
if (this.form.username === 'iview_admin') {
Cookies.set('access', 0);
} else {
Cookies.set('access', 1);
}
this.$router.push({
name: 'home_index'
});
}
});
}
});
}
}
};
</script>
INSTALLED_APPS = [
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
]
# http://www.django-rest-framework.org/api-guide/permissions/#api-reference
# rest-framework
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
# 'rest_framework.permissions.AllowAny',
'rest_framework.permissions.IsAuthenticated',
)
}
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
'localhost:8080',
)
APPEND_SLASH=False
from rest_framework.authtoken import views
path('api-token-auth', views.obtain_auth_token),
from .serializers import AssetSerializer
from rest_framework import permissions
from rest_framework import generics
from django.views.decorators.csrf import csrf_exempt
from rest_framework.pagination import PageNumberPagination
from django.utils.deprecation import MiddlewareMixin
class StandardResultsSetPagination(PageNumberPagination):
page_size = 2
page_size_query_param = 'page'
max_page_size = 1000
class DisableCSRFCheck(MiddlewareMixin):
def process_request(self, request):
setattr(request, '_dont_enforce_csrf_checks', True)
class AssetList(generics.ListCreateAPIView,DisableCSRFCheck):
queryset = AssetLoginUser.objects.all()
serializer_class = AssetSerializer
permission_classes = (permissions.IsAuthenticated,)
pagination_class = StandardResultsSetPagination
class AssetDetail(generics.RetrieveUpdateDestroyAPIView,DisableCSRFCheck):
queryset = AssetLoginUser.objects.all()
serializer_class = AssetSerializer
permission_classes = (permissions.IsAuthenticated,)
pagination_class = StandardResultsSetPagination
標題名稱:iview-admin1.3+django2.0(二)用戶登錄
文章網(wǎng)址:http://jinyejixie.com/article20/ggggco.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、域名注冊、微信小程序、用戶體驗、品牌網(wǎng)站設計、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)