3
votes

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...

2

2 Answers

1
votes

The easiest way to accomplish is to use sessions.

  • You need a session backend which stores your data on a server (see pyramid_redis_session). There are also cookie-based session solutions where all data is stored on the client side.

  • The first view writes all passed over data to a session:

    request.session["mydata"] = value
    
  • The second view reads data from the session

     print(request.session["mydata"])
    
0
votes

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').