from django.views.generic import DetailView, ListView
import taggit.models
-import models
+from . import models
+from . import views
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
- url(r'^$', ListView.as_view(
+ url(r'^$', views.static_context_list_view_factory(
+ extra_context={'title': 'Recipes'},
+ ).as_view(
queryset=models.Recipe.objects.all().order_by('name'),
context_object_name='recipes',
template_name='cookbook/recipes.html'),
url(r'^recipe/(?P<pk>\d+)/$', DetailView.as_view(
model=models.Recipe, template_name='cookbook/recipe.html'),
name='recipe'),
- url(r'^tags/$', ListView.as_view(
+ url(r'^tags/$', views.static_context_list_view_factory(
+ extra_context={'title': 'Tags'},
+ ).as_view(
queryset=taggit.models.Tag.objects.all(),
context_object_name='tags',
template_name='cookbook/tags.html'),
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
+from django.views.generic import ListView
from taggit.models import Tag
from .models import Recipe
return render_to_response(
'cookbook/recipes.html', {'recipes': recipes, 'title': tag.name},
RequestContext(request))
+
+class StaticContextListView (ListView):
+ _name_counter = 0
+ def get_context_data(self, **kwargs):
+ context = super(StaticContextListView, self).get_context_data(**kwargs)
+ context.update(self._extra_context)
+ return context
+
+def static_context_list_view_factory(extra_context):
+ class_name = 'StaticContextListView_{:d}'.format(
+ StaticContextListView._name_counter)
+ StaticContextListView._name_counter += 1
+ class_bases = (StaticContextListView,)
+ class_dict = dict(StaticContextListView.__dict__)
+ new_class = type(class_name, class_bases, class_dict)
+ new_class._extra_context = extra_context
+ return new_class