【发布时间】:2014-01-18 18:03:52
【问题描述】:
这个问题延伸 Django Pass Multiple Models to one Template
我想制作1个模板,即我的个人资料索引页面,所以它会列出url中指定的用户创建的所有对象
我想为一个模板查询多个模型,获取相关配置文件并显示由附加配置文件创建的所有 oferto 对象
views.py
class ProfileIndex(ListView):
context_object_name = 'account_list'
template_name = 'profiles/account_index.html'
queryset = Profile.objects.all()
def get_context_data(self, **kwargs):
context = super(ProfileIndex, self).get_context_data(**kwargs)
context['profile'] = Profile.objects.all()
context['oferto'] = Oferto.objects.get(kwargs.profile.slug)
return context
urls.py 我想我需要从 url 获取配置文件 slug 以便在 url 中搜索该用户的所有 oferto
url(
regex=r"^index/(?P<slug>[-_\w]+)",
view=ProfileIndex.as_view(),
name="profile_index",
),
这是最难弄清楚的,如何让我的模板在相关上下文中正常工作
{% block content %}
<h1>{{ profile }}<h1>
<h2> Ofertoj or/aŭ Offers</h2>
{% for oferto in profile %}
<a href="{% url "oferto_detail" oferto.slug %}">{{ oferto.name }}</a><br/>
{% endfor %}
{% endblock %}
【问题讨论】:
标签: python django django-class-based-views