Django 403 CSRF令牌丢失或不正确

时间:2013-08-14 01:51:10

标签: django csrf

我遇到过这个问题但不幸的是仍然不知道如何修复它。表单呈现完美,我输入信息并获得CSRF错误。给出的原因是token missing or incorrect

查看:

def eventSell(request, id):
    c = {}
    c.update(csrf(request))
    event = SquidEvent.objects.get(pk = id)
    listing_form = ListingForm(request.POST)
    if request.user.is_authenticated():
        if request.method == 'POST':
            listing_form = ListingForm(request.POST)
            if listing_form.is_valid():
                cd = listing_form.cleaned_data
                user = request.user
                item = Object(price = cd['price'], seller = user)
                item.save()
                return HttpResponseRedirect(reverse('tixeng:index'), c)
            #print listing_form
        else:
            return render_to_response('tixeng/list.html', {'event' : event, 'form' : listing_form}, c)
    else:
        return HttpResponseRedirect(reverse('allauth.account.views.login'))

这是我的模板:

<form action="{% url 'app:eventSell' event.id %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>

我认为我已经做好了一切,我不确定是什么导致了CSRF错误。此外,如果其相关的我跟随此作为指南:

http://www.djangobook.com/en/2.0/chapter07.html

2 个答案:

答案 0 :(得分:3)

这个堆栈Django "The view didn't return an HttpResponse object."能够帮助我解决这个问题。我将context_instance = RequestContext(request)添加到我的render_to_response后就可以了。

答案 1 :(得分:0)

自版本1.8起,

context_instance已弃用。 您可以使用:

return render(request, 'admin/match_main.html', RequestContext(request, locals()))