开发者

django: creating a generic form completion page

I have several forms in my application, and like all forms after the users have completed them, I would like to redirect them to a form completion page, which the particulars of the forms are displayed after the submission.

Now, assuming that my forms all have different attributes, how开发者_JAVA百科 can I create a common view and display template such that I do not have to re-create the them since they are all similar other than the particulars of the forms to be displayed?


According to this Django: How do I redirect a post and pass on the post data you can't redirect with the post data, so you have to either 1) display the success page at the same url as the form or 2) put all the variables into the redirect URL.

If you are ok displaying the success at the same URL as the form, i.e. /my-form/ not /my-form/success/, then you could mutate the canonical form processing view as such:

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            # ...
            return render_to_response('form_success.html', {'form' : form,} )
     else:
        form = ContactForm()    
    return render_to_response('contact.html', {'form': form,})

Then in the template you could do:

<h3>Success! You submitted:</h3>

{% for field in form %}
  {{field.label}}: {{field.data}}
{% endfor %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜