GG24
GG24

Reputation: 303

How to get value to one html page to another in Django?

I just began to work with Django and I would like to create a submit form in a html form that communicates with another html page.

Page1.html : Submit form, let's suppose I type "Hello world" then I click on submit, I'm redirected to page2.html

Page2.html : Thanks to the Get request Page2.html display "Hello World" because the page2 has been loaded from page1

In page1 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 to display how the word that has been typed in page1 in page2, thank you very much for your answer :)

Upvotes: 3

Views: 5903

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52133

Sending entire local variables to the template may be dangerous and insecure, as well as unnecessary so instead of passing locals(), you can either pass the entire querystring using request.GET or just the value keyed with 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 value in recherche.html like {{ search_box }}.

You can test it out by typing something like /page2?search_box=text into the address bar of your browser. The page should display text.

Upvotes: 3

Related Questions