【问题标题】:Django templates: How do I use a field in the model in a template?Django 模板:如何在模板中使用模型中的字段?
【发布时间】:2010-01-20 18:06:03
【问题描述】:

在这个模板中,

<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 %} 

我从搜索表单中选择了{{ first }}{{ last }},但没有选择其他参数,例如year_graduated

但我想说:

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

如何在模板中使用lawyer.year_graduated,即使它不在搜索表单中?

查看功能见my previous question

谢谢。

【问题讨论】:

  • 如果laywer.year_graduated 在所有结果中都不相同怎么办?您似乎在暗示 year_graduated 是一个搜索参数?
  • 是的,你是对的。我只在结果返回查询的同学的情况下使用这个。当有超过 1 名同名律师时,我只使用查询。

标签: django django-templates


【解决方案1】:

嗯,简单的方法就是将 year_graduated 添加到上下文字典中。

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

【讨论】:

    【解决方案2】:

    整个视图可以使用一些工作来使您自己更轻松一些。以下是一些快速更改(包括其他问题中讨论的更改):

    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, })
    

    因此,现在您可以使用“searched_lawyer.first”等,而不是在模板中使用“first”和“last”。(但这意味着您可以在您的模板中访问该律师的所有属性模板)

    【讨论】:

    • 看起来不错。我会详细研究这个并在我理解它时实施。感谢您的帮助。
    猜你喜欢
    • 2013-03-16
    • 2020-07-07
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2014-06-10
    相关资源
    最近更新 更多