我有一个使用Django-MPTT的模型,它还有一个通过template_name_field
使用特定模板的字段。我希望能够使用通用TemplateView
,但传入mixin以使用template_name_field
。
以下是模型:
from django.db import models
from mptt.models import MPTTModel
from mptt.fields import TreeForeignKey
class CategoryManager(models.Manager):
def get(self, **kwargs):
defaults = {}
defaults.update(kwargs)
if 'full_slug' in defaults:
if defaults['full_slug'] and defaults['full_slug'][-1] != "/":
defaults['full_slug'] += "/"
return super(CategoryManager, self).get(**defaults)
class Category(MPTTModel):
title = models.CharField(max_length=255)
description = models.TextField(blank=True, help_text='A brief description of the category. No HTML allowed.')
slug = models.SlugField(help_text='Prepopulates from title field.')
full_slug = models.CharField(max_length=255, blank=True)
template_name = models.CharField(max_length=70, blank=True, help_text="Example: 'categories/category_parent.html'. If this isn't provided, the system will use 'categories/category_detail.html'.")
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
objects = CategoryManager()
class Meta:
verbose_name = 'category'
verbose_name_plural = 'categories'
def save(self, *args, **kwargs):
orig_full_slug = self.full_slug
if self.parent:
self.full_slug = "%s%s/" % (self.parent.full_slug, self.slug)
else:
self.full_slug = "%s/" % self.slug
obj = super(Category, self).save(*args, **kwargs)
if orig_full_slug != self.full_slug:
for child in self.get_children():
child.save()
return obj
def __unicode__(self):
return "%s (%s)" % (self.title, self.full_slug)
以下是观点:
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.views.generic import TemplateView, DetailView
from django.views.generic.detail import SingleObjectTemplateResponseMixin
from django.utils.translation import ugettext as _
from django.contrib.syndication.views import Feed
from storefront.categories.models import Category
class SimpleCategoryView(TemplateView):
def get_category(self):
return Category.objects.get(full_slug=self.kwargs['full_slug'])
def get_context_data(self, **kwargs):
context = super(SimpleCategoryView, self).get_context_data(**kwargs)
context["category"] = self.get_category()
return context
这是我的网址:
from django.conf.urls.defaults import patterns, include, url
from storefront.categories.models import Category
from storefront.categories.views import CategoryView
urlpatterns = patterns('',
url(r'^(?P<full_slug>[-\w/]+)', SimpleCategoryView.as_view(template_name='categories/category_detail.html'), name='category_view'),
),
我如何添加mixin以将template_name_field
用于我的视图?
答案 0 :(得分:1)
您不需要单独的mixin。 TemplateView
已经有TemplateResponseMixin
,因此您只需覆盖get_template_names
子类中的SimpleCategoryView
。
class SimpleCategoryView(TemplateView):
...
def get_template_names(self):
return [self.get_category().template_name]
答案 1 :(得分:0)
DetailView
应该拥有您需要的一切。您只需要使用DetailView.get_template_names()
属性告诉template_name_field
哪个字段可以包含模板名称。下面是一个完整的视图定义,它根据url中的slug获取Category
。请注意,Category
将在模板中以object
和category
的形式提供。
class SimpleCategoryView(DetailView):
model = Category
slug_url_kwarg = 'full_slug'
slug_field = 'full_slug'
template_name_field = 'template_name'