在同一页面上有两个通用 ListView

kor*_*ora 0 python django listview

目前可以在活动下显示对象列表,但采访仍然是空的(尽管有与采访对象数量相对应的项目符号 - 只是文本没有显示)。

视图.py:

class IndexView(generic.ListView):
  template_name = 'expcore/index.html'
  model = Activity
  context_object_name = 'activities_list'
  queryset = Activity.objects.all()

  def get_context_data(self, **kwargs):
    context = super(IndexView, self).get_context_data(**kwargs)
    context['interviews_list'] = Interview.objects.all()
    return context
Run Code Online (Sandbox Code Playgroud)

索引.html:

  <div>
<h1>Activities</h1>
<ul>
{% for activity in activities_list %}
    <li><a href='/activity/{{ activity.name }}'></a></li>
{% empty %}
    <li>No activities available yet.</li>
{% endfor %}
</ul>
</div>

<div>
<h1>Interviews</h1>
<ul>
{% for interview in interviews_list %}
    <li><a href='/interview/{{ interview }}'>{{ interview.name }}</a></li>
{% empty %}
    <li>No interviews available yet.</li>
{% endfor %}
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)

为什么采访对象不显示的任何想法?

Isa*_*iev 6

中的覆盖get_context_data函数IndexView。在那里你可以发送多个对象。

class IndexView(generic.ListView):
  template_name = 'expcore/index.html'
  model = Activity
  context_object_name = 'activities_list'
  queryset = Activity.objects.all()

  def get_context_data(self, **kwargs):
    context = super(IndexView, self).get_context_data(**kwargs)
    context['interviews_list'] = Interview.objects.all()
    return context
Run Code Online (Sandbox Code Playgroud)

在模板上,您可以这样做。

活动用

{% for activity in activities_list %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

面试

{% for interview in interviews_list %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)