Django redirect to form page and submit the form?

By : Zeyu Hu

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.

By : Zeyu Hu


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:

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.


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

There's not a ton of information to go with here; but generally, if you want to stop looping when a condition is met, you can either break the loop or change a boolean variable which is checked in the while loop, preventing it from looping again.

The solution you suggest is a huge step in a right direction. The factory doesn't really smell like a locator, rather, it is a local factory, part of the domain it belongs to.

A step even further would be to forget the idea of family of factories (the interface) and have a concrete factory with pluggable implementation that internally uses a container (or doesn't use one) but offers a single api for its clients. This way you could remove the constructor injection of the factory into the form in favor of just using the factory's concrete type. The factory itself is configured in the Composition Root.

More details and code example in my blog entry

This video can help you solving your question :)
By: admin