Django templates: how do I save an item inside a {% for %} loop?

This application returns schoolmates of lawyers.

When a user searches for first name only and there are more than 1 result I leave the query in the form and ask them to enter a last name. But I thought that it would be nicer if the user just clicks to one of the names and search results are returned directly. (at this point the link takes you to search form again.)

In the template the query and the last name that I need are inside the {% for lawyer in lawyers %} loop as attributes: lawyer.first and lawyer.last. I couldn't figure out how to save them to create a query. Can you help me with how to solve this problem?

Note: The template is below but I put the view function in pastebin.com. It's 60 lines long, I wasn't sure if I should be posting it here.

Thank you.

<html>
<head>
    <title>Search results</title>
</head>
<body>

 <p>You searched for <strong>{{ first|capfirst }} </strong>.</p>

<p>There are {{ lawyers|length }} {{ first|capfirst }} in the database. Please select one.</p>

{% for lawyer in lawyers %}
    <ul><li><a href="/search/">{{ lawyer.first }} {{ lawyer.last }} {{ lawyer.firm_name }} {{ lawyer.school}} class of {{ lawyer.year_graduated }}</a></li></ul>
{% endfor %}

<form action="" method="get">
{{ form.as_p }}
<input type="submit" value="Submit">
    </form>
</body>
</html>

Edit

Do I need to create a new view function like this?

def new_query(request):
    last_name = request.GET.get('lawyers.last')
    first_name = request.GET.get('lawyer.first')
    ....

Answers


Since you are using GET for your form, you can completely emulate a form submission with a normal link.

<a href="?first_name={{lawyer.first}}&last_name={{lawyer.last}">

this will send a request to the current URL (because we're not specifying any URL, just GET parameters. this is the equivalent of

<form action="" method="get">

but adding to the form 2 parameters, called first_name, and last_name, just as they are called in your normal form - this way the same view can handle this request.

If however you have additional parameters in your form (like year_of_graduation), you'll have to add them too.


You could add them as GET parameters

<a href="/search/?first={{lawyer.first}}&last={{lawyer.last}">

Then through request.GET you could access the passed items.


Need Your Help

Can't find the go-pear.bat file PHP 5.5 Windows 7

php windows pear

The command from their documentation says to run the command:

MATLAB Anonymous function handle to use with NLFILTER

image matlab image-processing textures

I have the following function which calculates a GLCM and then a given statistic parameter. I would like to pass this function to NLFILTER to do the calculation for a whole image (in small windows,...