我想要以下内容:
一个模板视图(实际上是我的登录页面),其中包含许多项目,包括一个列表视图。
现在,我有一个列表视图,当我将它映射到它自己的 url 时它可以工作:
应用程序/views.py
class MymodelListView(ListView):
model = Mymodel
context_object_name = "mymodel_list"
应用程序/urls.py
app_name = "myapp"
urlpatterns = [
...
path("mlist/", MymodelListView.as_view(), name="mlist"),
...
]
应用程序/../mymodel_list.html
{% extends 'base.html' %}
{% block content %}
<ul>
{% for adventure in adventure_list %}
<li>
<a href="{{ adventure.get_absolute_url }}">{{ adventure.title }}</a>
</li>
{% endfor %}
</ul>
{% endblock content %}
现在这可以按预期工作,并在“mlist”链接中显示来自 Mymodel 的所有记录。
但是,我想将它嵌入到我的 index.html TemplateView 中。我的想法是使用一个片段并将其放在 index.html 中
{% include 'myapp/_mymodel_list.html' with mymodel_list=mymodel_list %}
(不确定是否需要“with”部分,但似乎我应该需要将列表从主模板传递到代码段。由于更基本的问题,目前无论如何都不起作用)。
那么我的主页视图设置如下:
class HomePageView(TemplateView):
template_name = "index.html"
def get_context_data(self, *args, **kwargs):
context = super(HomePageView, self).get_context_data(*args, **kwargs)
context["adventure_list"] = MymodelListView.get_context_data()
print(context)
return context
但这会崩溃:
Exception Type: TypeError at /
Exception Value: super(type, obj): obj must be an instance or subtype of type
如果我将 self 传递给MymodelListView.get_context_data(self)
.
现在据我所见, Mymodel 获取上下文很混乱,因为它正在传递一个 HomePage 上下文,而且有点错误。
我正在尝试做的事情是完全错误的吗?这就是为什么我无法用我的(通常相当可靠的)google-fu 找到任何有用的提示吗?如果是这样,我应该采取什么方法?
我想在其他页面中重新使用 Mymodel 列表视图(尽管可能会更改过滤条件),因此我尝试以 DRY 方式这样做。