Search code examples
frameworkspyramid

Passing data between form views in Pyramid


I'm making a project in pyramid framework, so i have a view which have a form in it with 2 input type texts and a submit button.

The form is a post method, so im getting them with a POST request.

I want to send them to a new view and display them on the screen. meaning:

  • on 0.0.0.0:6543 is the form on first view.

  • I want to display the values the user insert in the input on 0.0.0.0:6543/here

I tried with HTTPfound but i guess im missing an understanding on how to really pass the variables.

Please help me...


Solution

  • Another way to pass the data from one view to another is via the URL. This does not require server-side support, unlike sessions. Also, it's RESTful ;)

    return HTTPFound('/here?greeting=Hello&subject=World')
    

    In your second view you then simply get the variables from request.GET:

    greeting = request.GET.get('greeting', '')
    subject = request.GET.get('subject', '')
    # pass the data to the template
    return {
        "greeting": greeting,
        "subject": subject
    }
    

    Regarding your comment: You can't use HTTPFound with POST. You can, however, directly submit your form to /here using <form method="post" action="/here" ...>. In this case you'll be able to access the data using request.POST.get('greeting').