У меня есть модель, использующая Django-MPTT, в которой также есть поле для использования определенного шаблона через template_name_field
. Я хотел бы иметь возможность использовать общий TemplateView
, но передам миксин для использования 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
А вот мои URL:
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'),
),
Как мне добавить миксин для использования template_name_field
в моем представлении?