简体   繁体   English

modelformset_factory和csrf令牌丢失或不正确

[英]modelformset_factory and csrf token missing or incorrect

I am using model formsets to add multiple instances of a model at once. 我正在使用模型表单集一次添加一个模型的多个实例。 and I am using class based views. 我正在使用基于类的视图。 This is my views.py part for creating a "Library" 这是我的views.py部分,用于创建“库”

class LibraryCreate(View):
model = Library

def post(self, request, *args, **kwargs):
    LibraryFormSet = modelformset_factory(
        Library, form=create_library, extra=2)
    if request.method == 'POST':
        formset = LibraryFormSet(request.POST, request.FILES)
        if formset.is_valid():
            # do something with the formset.cleaned_data
            pass
    else:
        formset = LibraryFormSet()
    return render_to_response(
        'trial1/library_form.html', {'formset': formset})

def get(self, request, *args, **kwargs):
    LibraryFormSet = modelformset_factory(
        Library, form=create_library, extra=2)
    formset = LibraryFormSet(queryset=Library.objects.none())
    return render_to_response(
        'trial1/library_form.html', {'formset': formset})

and this is my template 这是我的模板

<form method="post" action="{% url "library_create" %}">
{% csrf_token %}
{{ formset.management_form }}
<table>
    {% for form in formset %}
    {{ form }}
    {% endfor %}
</table>
<input type="submit" value="create" />

now for some reason when I try to submit the form it returns a 403 forbidden because "CSRF token missing or incorrect.". 现在由于某种原因,当我尝试提交表单时,由于“ CSRF令牌丢失或不正确”,它返回了403禁止访问。 I don't get why this is not working and its getting really frustrating. 我不明白为什么这不起作用,并且它真的令人沮丧。

使用render而不是render_to_response以便将请求包含在模板上下文中。

return render(request, 'trial1/library_form.html', {'formset': formset})

You are missing the RequestContext object. 您缺少RequestContext对象。 The CSRF token is added by the CsrfMiddleware to the RequestContext object. CSRF令牌由CsrfMiddleware添加到RequestContext对象。 When you do not include the object, the token will be empty (inspect the form element in your browser and you will see it is missing). 当您不包含该对象时,令牌将为空(在浏览器中检查form元素,您将看到它丢失)。

https://docs.djangoproject.com/en/1.8/ref/templates/api/#django.template.RequestContext https://docs.djangoproject.com/en/1.8/ref/templates/api/#django.template.RequestContext

Use the render method or add the RequestContext to your view 使用render方法或将RequestContext添加到视图中

 return render_to_response('trial1/library_form.html',
                          {'formset': formset},
                          context_instance=RequestContext(request))

https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render-to-response (see context_instance attribute) https://docs.djangoproject.com/zh-CN/1.8/topics/http/shortcuts/#render https://docs.djangoproject.com/zh-CN/1.8/topics/http/shortcuts/#render-to-response (请参见context_instance属性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM