How to pass "invisible" variables from one page to another using a link in Django?

Asked

Viewed 21 times

-1

Suppose I have the following code in the.py urls; url(r'^relatorio', views.relatorioView, name='relatorio'), url(r'^relatorio/grafico', views.graph, name='grafico'),

On the report page I have a table with names and item codes. How can I make a link that redirects to the chart page of that particular item without leaving the explicit code in the url?

1 answer

0

You can make a POST, and send the data to the other view. It does the query set and returns a response, but as you sent it by POST, the identifiers will not appear in the url. But you have to be prepared for a get request, in the same view, in which case you can redirect the user to another part of the site.

This you can do with funcition views like this(this is in that documentation):

    if request.method == 'GET':
        do_something()
    elif request.method == 'POST':
       do_something_else()

With classes, you would use the appropriate methods for each type of request.

To pick up the amount passed to POST, you can do:

request.POST['chave-do-valor']

Use a form, to send the data, you must specify the enctype. For the above code to work.

<form action="/view-url" method="post" enctype="multipart/form-data">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.