Django cookie 没有保存在浏览器上 [英] Django cookies are not getting saved on browser

查看:47
本文介绍了Django cookie 没有保存在浏览器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 react 和 Django 制作一个应用程序.当我使用 django 登录时,我在 cookie 中设置了令牌,但未在浏览器中设置 Django 响应 cookie.我试图调试它,但不能.不知道我哪里做错了.

I am making an application with react and Django. When i login with django i set token in cookie but Django response cookies are not set in browser. I trying to debug it hard but could not. Don't know where i m doing wrong.

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

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.这里需要一些帮助.

There is no cookie saved in browser after completion of this request. Need some help here.

推荐答案

Found Solution was not sent withCredentials: true谢谢@sideshowbarker

Found Solution was not sending withCredentials: true Thanks @sideshowbarker

这篇关于Django cookie 没有保存在浏览器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆