2017-02-12 69 views
0

我正在做一个反应和Django的应用程序。当我用django登录时,我在cookie中设置了令牌,但是Django响应cookie没有在浏览器中设置。我试图调试它很难但不能。不知道我在哪里做错了。Django饼干没有保存在浏览器上

Request URL:http://localhost:8000/login/ 
Request Method:POST 
Status Code:200 OK 
Remote Address:127.0.0.1:8000 

Response Headers 
    Access-Control-Allow-Credentials:true 
    Access-Control-Allow-Origin:http://localhost:3000 
    Content-Type:application/json 
    Date:Sun, 12 Feb 2017 13:32:30 GMT 
    Server:WSGIServer/0.1 Python/2.7.10 
    Set-Cookie:token=97547ba32cb8abcfe81b28c47ee5e3b8087b54ac; Path=/ 
    Set-Cookie:sessionid=6twm2h9mad6q8k4w637ww25zt6l0ck2d; expires=Sun, 26-Feb-2017 13:32:30 GMT; httponly; Max-Age=1209600; Path=/ 
    Vary:Cookie 
    X-Frame-Options:SAMEORIGIN 

Request Headers 
    Accept:application/json, text/plain, */* 
    Accept-Encoding:gzip, deflate, br 
    Accept-Language:en-GB,en-US;q=0.8,en;q=0.6 
    Authorization:Token undefined 
    Connection:keep-alive 
    Content-Length:41 
    Content-Type:application/json;charset=UTF-8 
    Host:localhost:8000 
    Origin:http://localhost:3000 
    Referer:http://localhost:3000/ 
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 

客户端请求代码

export function adminSignIn({username, password}) { 
    return function (dispatch) { 
     axios 
      .post("http://localhost:8000/login/", {username, password}) 
      .then((response) => { 
       dispatch({type: ADMIN, payload: response.data}) 
       dispatch({type: HIDE_LOGGIN_MODAL, payload: response.data}) 
      }) 
      .catch((error) => { 
       console.log(error) 
      }) 
    } 
} 

settings.py

""" 
Django settings for server project. 

Generated by 'django-admin startproject' using Django 1.8. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.8/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.8/ref/settings/ 
""" 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '83vwkomf5s1y_!jt#=_dlgv!v0t38yl!a80h#r0buor70$0y7=' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'rest_framework', 
    'rest_framework.authtoken', 
    'corsheaders', 
    'inventory', 
    'home' 
) 

MIDDLEWARE_CLASSES = (
    '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', 
    'django.middleware.security.SecurityMiddleware', 
) 

REST_FRAMEWORK = { 
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', 
    'PAGE_SIZE': 40 
} 

CORS_ORIGIN_ALLOW_ALL = True 
CORS_ALLOW_CREDENTIALS = True 

# CSRF_COOKIE_NAME = "XSRF-TOKEN" 

ROOT_URLCONF = 'server.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'server.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.postgresql_psycopg2', 
     'NAME': 'bigbasket', 
     'USER': 'naresh', 
     'PASSWORD': 'naresh', 
     'HOST': 'localhost' 
     # 'PORT': '', 
    } 
} 


# Internationalization 
# https://docs.djangoproject.com/en/1.8/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.8/howto/static-files/ 

STATIC_URL = '/static/' 

没有这个请求完成后保存在浏览器cookie。在这里需要一些帮助。

回答

0

找到解决方案未送出withCredentials: true 谢谢@sideshowbarker