问题描述
我有 2 个模型,并且使用 get_context_data
方法使 IndexView
正常工作.但是,我使用相同技术的 DetailView
不起作用.如何简单地将 2 个模型放入 DetailView
?
I have 2 models and I got the IndexView
working properly using the get_context_data
method. However my DetailView
using the same technique is not working. How do I simply get 2 models into the DetailView
?
views.py
from .models import CharacterSeries, CharacterUniverse
class IndexView(generic.ListView):
template_name = 'character/index.html'
context_object_name = 'character_series_list'
def get_queryset(self):
return CharacterSeries.objects.order_by('name')
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['character_universe_list'] = CharacterUniverse.objects.order_by('name')
return context
class DetailView(generic.DetailView):
model = CharacterSeries
template_name = 'character/detail.html'
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['character_universe_list'] = CharacterUniverse.objects.all()
return context
我遗漏了一些东西;我需要将 CharacterUniverse
放入 DetailView.我试过使用 此页面的信息无济于事.
I am missing something; I need to get CharacterUniverse
into the DetailView. I have tried using this page's information to no avail.
谢谢大家.
更新:
detail.html
detail.html
<ul>
{% for series in characterseries.character_set.all %}
<li>{{ series.name }}</li>
{% endfor %}
</ul>
<ul>
{% for universe in characteruniverse.character_set.all %}
<li>{{ universe.name }}</li>
{% endfor %}
</ul>
index.html
{% load staticfiles %}
<link rel ="stylesheet" type="text/css" href="{% static 'character/style.css' %}" />
<h1>Character Series</h1>
<ul>
{% for character_series in character_series_list %} {# for MODEL in .. #}
<li><a href="{% url 'character:detail' character_series.pk %}">{{ character_series.name }}</a></li>
{% endfor %}
</ul>
<h1>Character Universe</h1>
<ul>
{% for character_universe in character_universe_list %} {# for MODEL in .. #}
<li><a href="{% url 'character:detail' character_universe.pk %}">{{ character_universe.name }}</a></li>
{% endfor %}
</ul>
官方文档中的一个例子:这对我来说是不可能的?
from django.views.generic import DetailView
from books.models import Publisher, Book
class PublisherDetail(DetailView):
model = Publisher
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(PublisherDetail, self).get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['book_list'] = Book.objects.all()
return context
也许这就是最终的解决方案?但这不起作用..命名错误?
Perhaps this is the final solution? This is not working though.. Naming wrong?
所有更新
views.py
from django.shortcuts import get_object_or_404, render
from django.views import generic
from django.views.generic import DetailView
from .models import CharacterSeries, CharacterUniverse
class IndexView(generic.ListView):
template_name = 'character/index.html'
context_object_name = 'character_series_list'
def get_queryset(self):
return CharacterSeries.objects.order_by('name')
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['character_universe_list'] = CharacterUniverse.objects.order_by('name')
return context
class SeriesDetail(DetailView):
model = CharacterSeries
template_name = 'character/series_detail.html'
class UniverseDetail(DetailView):
model = CharacterUniverse
template_name = 'character/universe_detail.html'
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.SeriesDetail.as_view(), name='series_detail'),
url(r'^(?P<pk>[0-9]+)/$', views.UniverseDetail.as_view(), name='universe_detail'),
]
index.html
{% load staticfiles %}
<link rel ="stylesheet" type="text/css" href="{% static 'character/style.css' %}" />
<h1>Character Series</h1>
<ul>
{% for character_series in character_series_list %}
<li><a href="{% url 'character:series_detail' character_series.pk %}">{{ character_series.name }}</a></li>
{% endfor %}
</ul>
<h1>Character Universe</h1>
<ul>
{% for character_universe in character_universe_list %}
<li><a href="{% url 'character:universe_detail' character_universe.pk %}">{{ character_universe.name }}</a></li>
{% endfor %}
</ul>
series_detail.html
<ul>
{% for series in characterseries.character_set.all %}
<li>{{ series.name }}</li>
{% endfor %}
</ul>
universe_detail.html
<ul>
{% for universe in characteruniverse.character_set.all %}
<li>{{ universe.name }}</li>
{% endfor %}
</ul>
推荐答案
解释为什么不能对两种模型使用一个视图
DetailView
用于显示一个模型中对象的详细信息.可以包含额外的上下文,但该视图并非旨在处理两种可能的模型.
Explanation of why you can't use one view for both models
A DetailView
is meant to display details about an object from one model. It's fine to include extra context, but the view isn't designed to handle two possible models.
文档中的示例显示一个出版商的详细信息,并同时显示所有书籍.
The example from the docs is showing the details for one publisher, and displaying all the books at the same time.
您的 DetailView 可让您显示一个 CharacterSeries 的详细信息,并同时显示所有 CharacterUniverse.
Your DetailView lets you show the details for one CharacterSeries, and display all of the CharacterUniverse at the same time.
但是,您不能使用同一个视图来显示一个 CharacterUniverse
的详细信息.您需要一个不同的视图来显示一个 CharacterUniverse
However, you cannot use that same view to display details for one CharacterUniverse
. You need a different view to display details for one CharacterUniverse
因此,您需要两个不同的详细视图,每个模型一个.
Therefore, you need two different detail views, one for each model.
每个视图都需要一个不同的网址.否则,请求将始终匹配第一个正则表达式(在本例中为 series_detail
.以下将起作用.
You need a distinct url for each view. Otherwise, the request will always match the first regex (in this case series_detail
. The following would work.
url(r'^series/(?P<pk>[0-9]+)/$', views.SeriesDetail.as_view(), name='series_detail'),
url(r'^universe/(?P<pk>[0-9]+)/$', views.UniverseDetail.as_view(), name='universe_detail'),
这篇关于多模型通用 DetailView 到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!