python - When tested http POST with chrome POSTMAN, it doesn't work in django -
i use django 1.9.7 & python 3.5
i implement creating user mechanism , tried test postman(chrome application), doesn't work , shows belows:
forbidden (csrf cookie not set.): /timeline/user/create/
this code :
urls.py
from django.conf.urls import url from. import views app_name = 'timeline' urlpatterns = [ # ex) / url(r'^$', views.timeline_view, name='timeline_view'), # ex) /user/create url(r'^user/(?p<method>create)/$', views.user_view, name='user_view'), ]
views.py
from django.contrib.auth import authenticate, login, logout django.shortcuts import render, httpresponse timeline.models import * def timeline_view(request): return httpresponse('hello world') def user_view(request, method): if method == 'create' , request.method == 'post': print("hi") username = request.post.get('username') username = request.post.get('username') user = user.objects.create_user(username, password=password) user.first_name = request.post.get('name','') user.save() profile = userprofile() profile.user = user profile.save() return httpresponse('create success') else: return httpresponse('bad request', status=400)
i tried django csrf cookie not set think post past version.
you should put csrftoken
in request headers.
after sending request via postman, @ response cookies
section, take csrftoken value , put in headers
section of request, this:
key:x-csrftoken
value: jsdh6c3vahglshleytjh2n957qcilqmb #your token value
Comments
Post a Comment