views:

85

answers:

2

Hello,

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')
    ....
+1  A: 

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.

czarchaic
Thanks. Do I create a new view function as I added in the edit to the question? Can you give more details or let me know where to look?
Zeynel
@Zeynel: No you don't need to create a new view. Just try it out.
Felix Kling
+1  A: 

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.

Ofri Raviv