Using django with postman {“detail”:“CSRF Failed: CSRF token missing or incorrect.”}

前端 未结 7 2220
谎友^
谎友^ 2021-02-20 17:08

I\'m using postman to check json response from my django-rest-framework.

When my first try to post id, email, password through POST method to my django on AWS(amazon web

7条回答
  •  野性不改
    2021-02-20 17:26

    In settings.py file

    INSTALLED_APPS = [
    ...
    ...
    ...
    ...
    'rest_framework.authtoken',
    ...
    ]
    
    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.TokenAuthentication',
        ),
    }
    
    

    in project urls.py

    from rest_framework.authtoken import views
    
    urlpatterns = [
        ....
        path('api-token-auth/',views.obtain_auth_token,name='api-token-auth')
    
    ]
    
    

    Open terminal as

    $ pip3 install httpie
    $ python3 manage.py createsuperuser # if not created
    $ http POST http://localhost:8000/api-token-auth/ username="username" password = "password"   # You will get token key (Just copy it) ex:a243re43fdeg7r4rfgedwe89320
    
    

    You token key will be also automatically saved in your databases

    Go to postman header (like in example) Ex: screenshot from postman ,where and how to paste accessed toke Then insert you token key.

    reference to get token key from this video

提交回复
热议问题