开发者

Django templates: How do I use a field in the model in a template?

In this template,

<body><p>You searched for: <strong>{{ first }} {{ last }}</strong></p>

{% if lawyers %}
    <p>There are {{ lawyers|length }} schoolmates of <strong>{{ first }} {{ last }}</stro开发者_Go百科ng> in the database:</p>
    <ul>
        {% for lawyer in lawyers %}
        <li> {{ lawyer.first }} {{ lawyer.last }} {{ lawyer.firm_name }} {{ lawyer.school }} {{ lawyer.year_graduated }}</li>
        {% endfor %}
    </ul>

{% else %}
    <p><strong>{{ first }} {{ last }}</strong> has no classmates in the database. Try another lawyer.</p>
{% endif %} 

I pick up {{ first }} and {{ last }} from the search form but not the other parameters such as year_graduated.

But I want to be able to say:

<p>You searched for: <strong> {{ first }} {{ last }}, class of {{ year_graduated }} </strong> </p>

How can I use lawyer.year_graduated in the template even though it is not in the search form?

See my previous question for the view function.

Thank you.


Well, the easy way would just be to add year_graduated to the context dict.

return render_to_response('search_results.html', {'lawyers': lawyers1, 'last': last_name, 'first': first_name, 'year_graduated': q_year[0], 'form': form})


The whole view could use some work to make things a little easier on yourself. Here are a few quick changes (including the ones discussed in the other question):

def search_form(request):
    if request.method == 'POST':
        search_form = SearchForm(request.POST)
        if search_form.is_valid():
            last_name = search_form.cleaned_data['last_name']
            first_name = search_form.cleaned_data['first_name']
            fields = {}
            if last_name:
                lawyers = fields['last__iexact'] = last_name
            if first_name:
                lawyers = fields['first__icontains'] = first_name
            try:
                searched_lawyer = Lawyer.objects.get(**fields)
            except Lawyer.DoesNotExist:
                form = SearchForm()
                return render_to_response('not_in_database.html', {'last': last_name, 'first': first_name, 'form': form})
            except Lawyer.MultipleObjectsReturned:
                form = SearchForm(initial={'last_name': last_name})
                # Note: this breaks the current multiple returns functionality, up to you...
                return render_to_response('more_than_1_match.html', {'last': last_name, 'first': first_name, 'form': form}) 
            q_school = searched_lawyer.school
            q_year = searched_lawyer.year_graduated
            classmates = Lawyer.objects.filter(school__iexact=q_school).filter(year_graduated__icontains=q_year).exclude(last__icontains=last_name)
            form = SearchForm()
            return render_to_response('search_results.html', {'classmates': classmates, 'searched_lawyer': searched_lawyer, 'form': form})
    else:
        form = SearchForm()
        return render_to_response('search_form.html', {'form': form, })

So now rather than using "first" and "last" in your template, you'd be using "searched_lawyer.first", etc.. (But this means you would have access to all the attributes of that lawyer in your template)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜