1

I want to display a value that user types

I am able to get the value using this lines:

    def on_submit_click(req):
        name = req.GET['search']
        return render(req, 'search_test.html')

But i'm not quite sure how to display it on the website

I tried doing this in the search_test.html:

     <h1 class="text-white text-center">{{ name }}</h1>

But it didn't work either.

1 Answer 1

1

You're missing the template context in your invocation of render(). render() takese these parameters:

render(request, template_name, context=None, content_type=None, status=None, using=None)

The template context is a dictionary with values which will replace the placeholders in the template. In your case, you want to replace the {{name}} with the search query param, I assume. So, your method should look like this:

    def on_submit_click(req):
        name = req.GET['search']
        return render(req, 'search_test.html', {"name": name}, content_type='application/xhtml+xml')

I've also set the content type

Sign up to request clarification or add additional context in comments.

1 Comment

@berkay8987 Hey, I'm glad it helped. Also, welcome to StackOverflow. it's common practice to also accept an answer that worked the best (if multiple) once your question is answered, by clicking on the green check mark. Happy coding :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.