Darwin Tech
Darwin Tech

Reputation: 18929

Django redirect invalid form to same page with GET variables

I have a Django view which corresponds to a URL like this:

<my_site>/project_info/?pitched=true

I validate the form for this page the normal way:

form = ProjectInfoForm(request.POST or None)
if form.is_valid():
    # handle form
    ...

But when the form is returned with errors it ends up at the same page without the URL (obviously):

<my_site>/project_info/

I don't really want to changed the pitched=true parameter to a captured URL parameter. What is the best way to redirect, considering I can't know if the page had this GET variable or not?

Upvotes: 1

Views: 1327

Answers (2)

FastRipeFastRotten
FastRipeFastRotten

Reputation: 1275

The action="" trick works. If you want to be sure, you can do something like

<form action="{% if request.GET.pitched %}?pitched={{ request.GET.pitched }}{% endif %}" method="post">

Upvotes: 2

girasquid
girasquid

Reputation: 15516

What does the HTML in your template look like? Try setting the action attribute on your form to "" (it should preserve your variables), like this:

<form action="" method="post">

Upvotes: 4

Related Questions