Forums

Django form sends data to own view

Hi,

I want to build a page with a form that can be filled out and which sends its data to the same view (context dictionary). So after you would hit the submit button the page would refresh and you would see the result below the form.

Typically you do a redirect to a new URL like in:

def form_view(request):
    if request.method == 'POST':
        form = Form_Name(request.POST)
        if form.is_valid()
            # redirect to a new URL:
            return HttpResponseRedirect('/new_view/')

So can I update the context dictionary and resend the user to

return HttpResponseRedirect('/form_view/')

?

Thanks

Just do something like

def form_view(request):
    if request.method == 'POST':
        # do whatever
        context = { data: 'whatever data you get out of the submitted form'}
    if request.method == 'GET':
        context = {}
        # do whatever else you need to do/add whatever other context etc
    return render(request, 'your_form.html', context)

It works, thanks!

Great, thanks for confirming!