1、下载django-vue-demo

从github中搜索django-vue-demo,clone到本地。前提是nodejs、vue、django都已安装好。当然我的环境和网上的例子不同。

2、配置环境

cd vueProject

  1. npm install
  2. npm run dev
  3. cd ../DjangoProject
  4. python manager.py runserver
  5. open http://127.0.0.1:8000/user

3、调试

  1. setting.py

  2. #MIDDLEWARE_CLASSES = [
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
    #    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',注释了这个
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    
    ]
    
#跨域增加忽略(增加这部分,不知是否起了作用)
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ()

CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)

CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
)

2.userapp\urls.py

from django.conf.urls import url
#import views
from . import views
urlpatterns = [
    url(r'^$', views.index, name="index"),
    url(r'^login$', views.login),
    url(r'^logout$', views.logout),
    url(r'^user_info$', views.get_user_info),
    url(r'^github_file_explorer$', views.github_file_explorer),
]
#新增以下内容
app_name='userapp'

3.views.py

#        json_data = json.loads(request.body)
        json_data = json.loads(request.body.decode('utf-8'))
#修改打印为以下
print(username, password)
#    if user.is_authenticated():
    if user.is_authenticated:

4.将templates\userapp\index.html移动到templates\下

results matching ""

    No results matching ""