简体   繁体   中英

Django 403 Error while using POST method through Front end ( Angular Js )

views.py

class ExtendUserSession(MiddlewareMixin):
    """
    Extend authenticated user's sessions so they don't have to log back in
    next 15 minutes (set by Django's default `SESSION_COOKIE_AGE` setting). 
    """
    def process_request(self, request):
        # Only extend the session for auth'd users
        if request.user.is_authenticated:
            reason = CsrfViewMiddleware('get_response').process_view(request, None, (), {})
            if reason:
                # process_view returns HTTPException
                pass
        else:
                # process_view returns None - No error on HTTP request from CSRF middleware verification
            request.session.set_expiry(86400)

settings.py

CORS_ORIGIN_WHITELIST = [
    
    'http://localhost:8000',   #I have changed the localhost as in my local ip. so 8000 is backend and 8080 is frontend port 
    'http://localhost:8080',
    

]
# A list of origins that are allowed to make cross-site HTTP requests to your Django application.
CSRF_TRUSTED_ORIGINS = [
    'http://localhost:8000',
    'http://localhost:8080',
]


CSRF_WHITELIST_ORIGINS = ['localhost:8000','localhost:8080']
# SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
# SESSION_COOKIE_NAME='sessionid'
SESSION_COOKIE_PATH='/' #default /
# SESSION_COOKIE_SECURE=True#default False
SESSION_COOKIE_SECURE=False
# SESSION_COOKIE_DOMAIN='localhost:8080' #default None
SESSION_COOKIE_DOMAIN= None
SESSION_COOKIE_HTTPONLY=False  # default is True
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
SESSION_COOKIE_AGE = 86400 
CSRF_COOKIE_NAME='X-CSRFToken'
CSRF_COOKIE_AGE=86400

CSRF_COOKIE_DOMAIN=None
CSRF_COOKIE_HTTPONLY=False
CSRF_COOKIE_PATH='/'
CSRF_COOKIE_SECURE=False




I'm able to get post method from POSTMAN for eg: 在此处输入图像描述

But while trying through Front-end this is the error I'll be getting

在此处输入图像描述

在此处输入图像描述

Is there any settings I have to change for making it accepting request through FrontEnd

Probable issue could be the front end is not sending the "sessionid" cookie which is used to verify authentication in back end. So backend redirects to login page as it does not see user as authenticated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM