tags:

views:

33

answers:

1

I want to pass a form's field value to the next page (template) after user submit the page, the field could be user name, consider the following setup

def form_submit(request):
    if request.method == 'POST':
        form = UsersForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            try:
                newUser = form.save()
                return HttpResponseRedirect('/mysite/nextpage/')
            except Exception, ex:
                return HttpResponse("Error %s" % str(ex))
        else:
            return HttpResponse('Error')

"nextpage" is the template that renders after user submit the form, so I want to know how to append the form's field (user name) to the url and get that value from the view in order to pass it to the next page..

thanks.

+2  A: 

Change redirect in your controller to include the user name (I guessed at the field name):

def form_submit(request):
    if request.method == 'POST':
        form = UsersForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            try:
                newUser = form.save()
                return HttpResponseRedirect('/mysite/nextpage/{0}/'.format(newUser.UserName)
            except Exception, ex:
                return HttpResponse("Ane apoi %s" % str(ex))
        else:
            return HttpResponse('Error')

Then you will need a new controller:

def user_redirected_here(request, username):
    #do something

And finally add something like this to your urls.py to route to the new view:

urlpatterns = patterns('',
    (r"^nextpage/.+$", user_redirected_here),
)
Jason Webb
I'm getting this error, user_redirected_here() takes exactly 2 arguments (1 given)
MMRUser
figured it out the url config should be (r'^nextpage/(?P<username>\w+)/$', views.user_redirected_here)
MMRUser