多个模型通用DetailView到模板

时间:2015-06-30 18:58:11

标签: django django-views

我有2个模型,class Posting(object): def __init__(self): self.available_postings = {} self.candidates = {} def add_post(self, firm): self.available_postings[firm.get_firm_id()] = firm def add_candidate(self, agent): self.candidates[agent.get_id()] = agent def assign_post(self): # Rank positions by wage and rank employees by qualifications # Make a match while len(self.candidates) > 0 and len(self.available_postings) > 0: # Best qualification dummy_best = self.candidates.popitem() for key in self.candidates.keys(): if dummy_best.get_qual() > self.candidates[key].get_qual(): dummy_best = self.candidates[key] # Higher wage dummy_higher = self.available_postings.popitem() for key in self.available_postings.keys(): if dummy_higher.set_wage_base() > self.available_postings[key].set_wage_base(): dummy_higher = self.available_postings[key] # Assignment. Firm has method add_employee dummy_higher.add_employee(dummy_best) # Remove agent and firm from list del self.available_postings[dummy_higher.get_firm_id()] del self.candidates[dummy_best.get_id()] 使用IndexView方法正常工作。但是我使用相同技术的get_context_data无效。如何将{2}模型简化为DetailView

views.py

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 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 放入DetailView。我尝试使用this page的信息无济于事。

谢谢大家。

更新

detail.html

CharacterUniverse

的index.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>

官方文档中的一个例子:在我的情况下这是不可能的?

{% 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>

也许这是最终的解决方案?这不行,但命名错了?

所有更新

views.py

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

urls.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'

的index.html

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

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

universe_detail.html

<ul>
{% for series in characterseries.character_set.all %}
    <li>{{ series.name }}</li>
{% endfor %}
</ul>

1 个答案:

答案 0 :(得分:4)

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

DetailView用于显示一个模型中有关对象的详细信息。包含额外的上下文很好,但视图不是为处理两种可能的模型而设计的。

文档中的示例显示了一个发布者的详细信息,并同时显示了所有图书。

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

但是,您无法使用同一视图显示一个CharacterUniverse的详细信息。您需要一个不同的视图来显示一个CharacterUniverse

的详细信息

解决方案

因此,您需要两个不同的详细视图,每个模型一个。

每个视图都需要一个不同的网址。否则,请求将始终与第一个正则表达式匹配(在本例中为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'),