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 }}</strong> 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
.
我从搜索表单中获取{first}和{last},但不从其他参数(如year_graduation)中获取。
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?
我如何使用律师。year_graduated在模板中,即使它不在搜索表单中?
See my previous question for the view function.
请参阅我前面关于视图函数的问题。
Thank you.
谢谢你!
2 个解决方案
#1
2
Well, the easy way would just be to add year_graduated to the context dict.
好的,简单的方法是将year_graduation添加到context dict语句中。
return render_to_response('search_results.html', {'lawyers': lawyers1, 'last': last_name, 'first': first_name, 'year_graduated': q_year[0], 'form': form})
#2
0
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)
因此,现在您应该使用“searched_lawyer”而不是在模板中使用“first”和“last”。第一”等。(但这意味着您可以访问模板中律师的所有属性)
#1
2
Well, the easy way would just be to add year_graduated to the context dict.
好的,简单的方法是将year_graduation添加到context dict语句中。
return render_to_response('search_results.html', {'lawyers': lawyers1, 'last': last_name, 'first': first_name, 'year_graduated': q_year[0], 'form': form})
#2
0
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)
因此,现在您应该使用“searched_lawyer”而不是在模板中使用“first”和“last”。第一”等。(但这意味着您可以访问模板中律师的所有属性)