0

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.

3
  • 2
    you should show some code showing what you have tried and also to give us a better idea of what you want.
    – Jacobr365
    Commented Sep 28, 2016 at 18:14
  • I don't record all the code changes. I will try to explain more clearly
    – Zeyu Hu
    Commented Sep 29, 2016 at 16:22
  • I got this solved by making the hyperlink using POST method using javascript. It may be not the best solution but do solve my problem.
    – Zeyu Hu
    Commented Sep 29, 2016 at 16:46

1 Answer 1

0

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.

E.g. 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.

E.g.

<a href="{% url 'search_page' %}?searchterm=232> Item 232 </a>
2
  • Is there a way to keep the original search using POST method? My idea right now is using different a new url to catch that and redirect to the search page as if the call is made on search page.
    – Zeyu Hu
    Commented Sep 29, 2016 at 16:25
  • I would highly recommend using the form with GET if the information being handled is not sensitive. Please refer to this link to view differences in GET and POST: w3schools.com/tags/ref_httpmethods.asp
    – zubhav
    Commented Sep 29, 2016 at 18:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.