【问题标题】:Inserting ListView Template in a section of the Homepage using Django 1.11使用 Django 1.11 在主页的一部分中插入 ListView 模板
【发布时间】:2018-09-02 14:17:32
【问题描述】:

我在 Django 1.11 中有一个呈现为 group_list.html 的 ListView 模板。但是我希望在主页的列中显示相同的视图

如果这有助于理解我想要实现的目标,我也有下面的屏幕截图

我尝试了不同的方式来使用{% include 'some.html' %} 1)我尝试制作一个html页面并将其包含在主页中。但它不断给出错误 2)我尝试将 group_list.html 页面更改为(插入模板)并尝试将其插入到索引页面中并为组创建另一个模板视图。但这也给出了错误

Below are my views and templates

Views.py

 class GroupCreate(LoginRequiredMixin, CreateView):
    model = Group
    fields = ('name', 'description')


class GroupList(ListView):
    model = Group


class GroupDetail(DetailView):
    model = Group

下面是我的 Index.html 和 group_list.html

INDEX.HTML

    {% extends 'base.html' %}

{% block body %}
    <div class="col-md-8" style="background-color:white; border-right:1px solid grey;">
        <h4>Groups</h4>
        <p>Join a group or make a new Group.</p>
            <a style="float:left" class="btn btn-success" href="{% url 'groups:new' %}">Start a Group</a>
        <div class="content">
            <!--{ % include 'groups/some.html' % } I am trying to insert my -->
        </div>
    </div>


<div class="col-md-4" style=background-color:white">
    <h4>Second Coloumn</h4>

</div>
{% endblock %}

下面是我的 group_list.html

{% extends 'groups/group_base.html' %}
{% block pre_group %}
<div class="col-md-4">
    <div>
        <h2>Welcome Back
        <a href="{% url 'posts:for_user' username=user.username %}">{{group.user.username}}</a>
        </h2>
        <h3>Groups</h3>
        <p>Welcome to the groups page! Select the Group with the shared interest</p>
    </div>
    <div>
        {% if user.is_authenticated %}
        <a class="btn btn-warning" href="{% url 'groups:new' %}"><span class="glyphicon glyphicon-plus-sign"></span> Create a New Group</a>
        {% endif %}
    </div>

</div>

{% endblock %}
{% block content %}
<div class="col-md-8">
    <div class="list-group">
        {% for group in object_list %}
        <a class="list-group-item" href="{% url 'groups:single' slug=group.slug  %}">
            <h3 class="list-group-item-heading">{{group.name}}</h3>
            <div class="list-group-item-text container-fluid">
                {{group.description_html|safe}}
                <div class="row">
                    <div class="col-md-4">
                        <span class="badge">{{group.members.count}}</span> member{{group.members.count|pluralize}}
                    </div>
                    <div class="col-md-4">
                        <span class="badge">{{group.posts.count}}</span> post{{group.posts.count|pluralize}}
                    </div>
                </div>
            </div>
        </a>
        {% endfor %}
    </div>
</div>

{% endblock %}

主页视图来自它们下面的根视图

View.py(根目录)

from django.views.generic import TemplateView

class Homepage(TemplateView):
    template_name = 'index.html'

class LoggedIn(TemplateView):
    template_name = 'test.html'

class LoggedOut(TemplateView):
    template_name = 'thanks.html'

在我的第一个独立项目工作时,我仍在学习 Django。我做了一些研究,但无法解决这个问题

I want a list view like this to show on a section of the HomePage

Currently the view works perfectly well on the groups page

【问题讨论】:

  • 请显示您主页的视图
  • @Alasdair 我已经添加了上面首页的视图,感谢帮助解决这个问题

标签: python django django-templates django-views


【解决方案1】:

在您的主页视图中,您可以覆盖 get_context_data 并将组添加到上下文中。

class Homepage(TemplateView):
    template_name = 'index.html'

    def get_context_data(self, **kwargs):
        context = super(Homepage, self).get_context_data(**kwargs)     
        context['object_list'] = Group.objects.all()
        return context

index.html 中,您必须从group_list.html 模板中复制代码。为防止重复,您可以将代码从内容块移动到 include_groups.html 模板中,然后使用 {% include 'include_groups.html' %} in yourindex.htmlandgroup_list.html` 模板。

在这种情况下,由于您的 Homepage 视图只是一个模板视图,您可以改用 ListView

class Homepage(ListView):
    model = Group
    template = 'index.html'

无论您选择哪个选项,了解如何在基于类的视图中覆盖 get_context_data 都会很有用。

【讨论】:

  • 是的。谢谢!!
猜你喜欢
  • 2018-02-22
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 2017-11-13
  • 2018-02-14
相关资源
最近更新 更多