简体   繁体   中英

Django redirect to form page and submit the form?

Right now. I have a search function in my page to search for item id. When I click search, I will render the same page with the result items and show item. And in other pages where I also display the item id, I want to add a link to the id to go to the same page where I search for that id.

Example: id: 123, I want the same page when: 1. search '123' in my search page(my search only accept exact match) 2. In other pages, click '123', go to the search page with results

How should I achieve this, I have tried many ways which don't wok.

You need to make use of the GET method that HTML forms provide. When you perform a search from the first page, you must make sure that you are doing so using the GET method in the form. This will append the form data into the URL.

Eg If you have a 'name' field in your form which has 'John' inputted. The submission of this form will compose a URL like so:

http://someurl.com/?name=John

This can then be accessed using the Django request object:

name = request.GET['name']

You've probably done something similar already for displaying your search results. So, all you need to do is create a link in your second page that redirects to the search page with GET request variables appended.

Eg

<a href="{% url 'search_page' %}?searchterm=232> Item 232 </a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM