Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6804621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:29:20+00:00 2026-05-26T19:29:20+00:00

I have a model using Django-MPTT that also has a field for using a

  • 0

I have a model using Django-MPTT that also has a field for using a specific template via the template_name_field. I’d like to be able to use a generic TemplateView, but pass in the mixin for using the template_name_field.

Here’s the model:

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)

Here’s the view:

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

And here’s my URLS:

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

),

How would I go about adding the mixin for using the template_name_field to my view?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:29:20+00:00Added an answer on May 26, 2026 at 7:29 pm

    You don’t need a separate mixin. TemplateView already has the TemplateResponseMixin, so you can simply override get_template_names in your SimpleCategoryView subclass.

    class SimpleCategoryView(TemplateView):
        ...
        def get_template_names(self):
            return [self.get_category().template_name]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Django 1.2.4. I have an admin class for a model that has
I'm using django/apache/sqlite3 and I have a django model that looks like this: class
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
Am using django forms on my app. I have a model Client like this:
I am using Django. I have a Fixture model, which looks something like: class
I have a Django project, that has a Address model. This is used in
I want to generate a list using my django model say I have these
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
I'm using ASP.NET MVC and have a model which has an image (byte array)
I'm using django-mptt to manage a simple CMS, with a model called Page ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.