多个模型通用的DetailView到模板 [英] Multiple models generic DetailView to template

查看:60
本文介绍了多个模型通用的DetailView到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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

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?

所有更新

ALL THE UPDATES

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>


推荐答案

为什么不能使用一个视图两种模型



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

每个视图都需要一个不同的URL。否则,请求将始终与第一个正则表达式匹配(在这种情况下,为 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到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆