Django 403 CSRF token missing or incorrect

五迷三道 提交于 2019-12-23 15:56:19

问题


I've encountered this issue but unfortunately still do not know how to fix it. The form renders perfectly, I enter the info and get a CSRF error. The reason given is token missing or incorrect.

View:

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'))

Here is my template:

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

I think I've done everything right, I'm not sure what's causing the CSRF error. Also, in case its relevant I was following along with this as a guide:

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


回答1:


This stack here Django "The view didn't return an HttpResponse object." was able to help me sort it out. Once I added context_instance = RequestContext(request) to my render_to_response it worked.




回答2:


context_instance is deprecated since version 1.8. You can use:

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


来源:https://stackoverflow.com/questions/18221980/django-403-csrf-token-missing-or-incorrect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!