Django-uni-form helpers and CSRF tags over POST

I use django-uni-forms to display my fields, with a rather rudimentary example from their book.

When I process the form fields with <form>{%csrf_tag%} {%form|as_uni_form%}</form>, everything works as expected.

However, the django-uni-form helpers allow you to generate a form tag (and other helpers related content) using the following syntax {% with form.helper as helper %}{% uni_form form helper%}{%endwith%}- this creates a tag for me <form>, so nowhere to embed my own CSRF_token. When I try to use this syntax, the form will display fine, but without the CSRF token, and therefore submitting the form fails every time.

Does anyone have any experience? Is there an established way to add a token? I prefer the second syntax for reuse. Thank!

+3
source share
4 answers

Have you checked the source? He should already be there. The uni_form tag template should include it automatically.

+3
source

I have the same problem with django-uni-form. The csrf current is not displayed after the tag <form>if I use:

{% load uni_form_tags %}
{% uni_form form helper %}

or

{% load uni_form_tags %}
{% with form.helper as helper %}
    {% uni_form form helper%}
{%endwith%}

If I turn it on manually, it works:

<form action='{{ request.path }}' method='POST' class="uniForm">{% csrf_token %}
{{ form|safe }}
</form>

I found a blog post that describes how to manually enable the csrf token:

helper = FormHelper()

csrf_token = Hidden(
                name = 'csrfmiddlewaretoken',
                value = request.META['CSRF_COOKIE'])
helper.add_input(csrf_token)

Not at all, but at least he gets unification.

+2
source

The easiest solution is to install django-uni-form from GitHub until the version of PyPi is updated to 0.8.

pip install https://github.com/pydanny/django-uni-form/tarball/master
+1
source

Use the latest version of django-uni-form. He corrects this and more.

+1
source

Source: https://habr.com/ru/post/1787220/


All Articles