modelFormSet and csrf

24 views
Skip to first unread message

joulumaa

unread,
Feb 24, 2015, 12:09:00 AM2/24/15
to django...@googlegroups.com
Hi,
I just studied and created first modelFormSet, and tried to use it in view.
I have same code in template as is in django documentation.
modelFormset shows data ok, but submit button is missing,why it is not in example template in documentation?
ok, I added submit button like I have used with basic forms, it shows up but cause csrf error,
then I added csrf tag as in form cases before, but still csrf  error....

I am missing some information i guess???, any help for beginner...?

-Vesa
btw, i would like to have that saving button on each row in formset.... is it possible?

aRkadeFR

unread,
Feb 24, 2015, 8:31:13 AM2/24/15
to django...@googlegroups.com
Hello :)

The template indeed doesn't show the submit button in the
documentation here:
https://docs.djangoproject.com/en/1.7/topics/forms/formsets/#using-a-formset-in-views-and-templates

In order to add the csrf token, you can use the template tag
{% csrf %} inside the form tag.
And add the submit button before closing the form tag too :)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ee930575-0a61-4c74-ad34-847df414f59c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

joulumaa

unread,
Feb 24, 2015, 8:55:41 AM2/24/15
to django...@googlegroups.com, con...@arkade.info
Thanks, but I did exactly that and still 403 csrf error, attached my template and related code
thanks for help

<form method="post" action="">
{% csrf_token %}   
    <table>
        {{ formset }}
    </table>
 <input type="submit" value="Submit" />
</form>

def school(request):
    SchoolFormSet = modelformset_factory(SchoolHistory)
    if request.method == 'post':
        formset = SchoolFormSetSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            # do something.
    else:
        formset = SchoolFormSet()
    return render_to_response("testformset.html", {
        "formset": formset,
    })

aRkadeFR

unread,
Feb 24, 2015, 10:54:39 AM2/24/15
to joulumaa, django...@googlegroups.com
Please, print your variable request.POST on your
view and see if the csrf_token is missing or not?
Same for your formset variable :)

joulu tervehdys

unread,
Feb 24, 2015, 11:55:21 AM2/24/15
to aRkadeFR, django...@googlegroups.com
Hi,Yes csrf was missing, but got it working by changing render_to_response to render.
Surprising that example code was not designed to work as such

Still I would like to get action buttons for each row. Admin view has at least some kind of links in each row.
-thanks
Reply all
Reply to author
Forward
0 new messages