[英]Django: CSRF cookie not set
我想在django項目中發布一個表單,導致“ CSRF cookie未設置”。 錯誤。
from django.shortcuts import render
from django.template import RequestContext
from pyBikeMilesApp.models import Eintrag
from django.template.context_processors import csrf
# Create your views here.
def index(request):
if request.method == 'POST':
print(request.POST)
# Get all posts from DB
eintraege = Eintrag.objects
return render(request,'index.html',{'Eintraege': eintraege})
{% extends 'base.html' %}
{% block title %}BikeMiles{% endblock %}
{% block content %}
<div class="col-md-12">
<table class="table table-striped">
<tr>
<th>gefahrene KM</th>
<th>Datum</th>
<th>Kommentar</th>
<th>Aktion</th>
{% for eintrag in Eintraege %}
<tr>
<td>{{ eintrag.kommentar }}</td>
<form method="post" action="http://127.0.0.1:8000/">
{% csrf_token %}
<input type="hidden" name="id" value="{{eintrag.id}}">
<input type="submit" class="btn btn-xs btn-danger" value="delete" >
</form>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
在生成的頁面中,我可以看到csrf輸入字段:
<input type='hidden' name='csrfmiddlewaretoken' value='zzRnENjz6wrUP8Op8IVOIDsUVvclY37k' />
任何想法,如何解決?
我前一陣子遇到了這個問題: Django-由於csrf令牌在Windows上不起作用,AJAX無法正常工作
簡而言之,將@ensure_csrf_cookie
放在視圖定義之前。
即
# Create your views here.
@ensure_csrf_cookie
def index(request):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.