Python – How do I pass the value of one html page to another page in Django?

How do I pass the value of one html page to another page in Django?… here is a solution to the problem.

How do I pass the value of one html page to another page in Django?

I

just started using Django and I want to create a submit form for an html form that communicates with another html page.

Page1.html : Submit the form, let’s say I type “Hello world” and click submit, I am redirected to page2.html

Page2.html : Page2.html displays “Hello World” because Page2 is loaded from page1 due to the Get request

On page 1 I use this form:

<form type="get" action="/page2" style="margin: 0">
<input  id="search_box" type="text" name="search_box"  placeholder="Search..." >
<button id="search_submit" type="submit" >Search</button>

In Views.py I have this function:

def page2(request):
    if request.method == 'GET':
        search_query = request. GET.get('search_box', None)
        return render(request, 'interface/recherche.html', locals())

But I don’t know how the words entered in page1 show up in page2, thank you very much for your answer 🙂

Solution

Sending entire local variables to templates can be dangerous and unsafe, and unnecessary, so in addition to passing locals(), you can use request. GET< PASS THE ENTIRE QUERY STRING OR JUST TYPE THE VALUE IN search_box:

return render(request, "interface/recherche.html", request. GET)

or

return render(request, "interface/recherche.html", {"search_box": search_query})

And you can print out the values in recherche.html, such as {{ search_box }}.

You can test it by typing something like /page2?search_box=text in your browser’s address bar. The page should display text.

Related Problems and Solutions