cs-student
cs-student

Reputation: 33

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:

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

Please help me...

Upvotes: 3

Views: 806

Answers (2)

Sergey
Sergey

Reputation: 12417

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

Upvotes: 0

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83398

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"])
    

Upvotes: 1

Related Questions