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

mipsel-gdb breakpoint from source code

gdb breakpoints mips

It seems that gdb is failing to find breakpoints if a class is implemented in the header file. Therefore I am trying to force a breakpoint from the source. I guess I need the mips equivalent of asm...