【发布时间】: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
【问题讨论】:
-
请显示您主页的视图
-
@Alasdair 我已经添加了上面首页的视图,感谢帮助解决这个问题
标签: python django django-templates django-views