gpt4 book ai didi

django - 多模型通用 DetailView 到模板

转载 作者:行者123 更新时间:2023-12-04 21:59:02 24 4
gpt4 key购买 nike

我有 2 个模型,我得到了 IndexView使用 get_context_data 正常工作方法。然而我的 DetailView使用相同的技术是行不通的。我如何简单地将 2 个模型放入 DetailView ?

View .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进入细节 View 。我试过使用 this page的信息无济于事。

谢谢大家。

更新:

详细信息.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>

索引.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

也许这就是最终的解决方案?虽然这不起作用..命名错误?

所有更新

View .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>

最佳答案

解释为什么不能对两个模型使用一个 View

一个 DetailView旨在显示有关一个模型中对象的详细信息。包含额外的上下文很好,但该 View 并非旨在处理两种可能的模型。

文档中的示例显示一个出版商的详细信息,并同时显示所有书籍。

您的 DetailView 允许您显示一个 CharacterSeries 的详细信息,并同时显示所有 CharacterUniverse。

但是,您不能使用相同的 View 来显示一个 CharacterUniverse 的详细信息。 .您需要一个不同的 View 来显示一个 CharacterUniverse 的详细信息

解决方案

因此,您需要两个不同的局部 View ,每个模型一个。

每个 View 都需要一个不同的 url。否则,请求将始终匹配第一个正则表达式(在本例中为 series_detail 。以下将起作用。

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'),

关于django - 多模型通用 DetailView 到模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31146275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com