当我尝试从我的反应应用程序中向django视图发出一个简单的帖子请求时,我得到403.这是我的代码:
views.py
@csrf_protect
def test_view():
if (request.method == 'POST'):
return HttpResponse(request.body)
Run Code Online (Sandbox Code Playgroud)
Login.js(React组件)
import Cookies from 'js-cookie';
//React constructor {
test_view() {
const csrftoken = Cookies.get('csrftoken');
const config = {
headers: {'HTTP_X_CSRFTOKEN': csrftoken},
}
axios.post('/prototype/hello/', {firstName: 'Fred'}, config)
.then(res => {console.log("Test res: " + res.data)});
}
//}
Run Code Online (Sandbox Code Playgroud)
urls.py
url(r'^hello', views.test_view, name='test-view'),
Run Code Online (Sandbox Code Playgroud)
'js-cookie'库有可能不起作用吗?我没有{%csrf_token%},因为我没有使用除index.html之外的django模板.相反,我有@csrf_protect装饰器.我认为这就是我应该根据文档做的事情.