If you create a Django library with views, always accept extra context and pass it on to the template. It's just courtesy.
Similarly, allow them to override the form class with their own (they should subclass your form class to do this; if they don't they have no one but themselves to blame if it doesn't work).
This way, if people ever make more custom use of your views (and they might!), they can add the extra values they need to make your otherwise excellent library work just right for them.
This is very easy to implement if you're still using functional views.
Instead of:
from .forms import ObjectForm def edit_view(request, object_slug=None): form = ObjectForm() ... ctx = { 'form': form, 'etc': ... } return render(request, template_name, ctx)
You'd use:
from .forms import ObjectForm def edit_view(request, object_slug=None, form_class=ObjectForm, extra_context=None): form = ObjectForm() ... ctx['form'] = form ctx['etc'] = ... ctx.update(extra_context) return render(request, template_name, ctx)
Users can then customize your view thusly:
from yourapp.views import edit_view from foo.forms import CustomizedForm urlpatterns = patterns('', # deprecated in 1.8+ url( '^yourapp/(?P<object_slug>[^/]+)/edit/$', edit_view, { 'form_class': CustomizedForm, 'extra_context': { 'base_template': 'special_base.html' } } )
Class views are a bit trickier. If you set the form_class in the View itself (not uncommon), then the only way they can override it is by subclassing your view. However, you shouldn't require them to subclass your view just to send along extra context. This code will do that:
from .forms import ObjectForm from django.views.generic import UpdateView class UpdateObjectView(UpdateView): form_class = ObjectForm def get_context_data(self, **kwargs): context = super(UpdateObjectView, self).get_context_data(**kwargs) context.update(self.kwargs.get('extra_context', {})) return context
Then, to customize, they do this:
from yourapp.views import UpdateObjectView from foo.forms import CustomizedForm class CustomizedUpdateObjectView(UpdateObjectView): form_class = CustomizedForm urlpatterns = patterns('', # deprecated in 1.8+ url( '^yourapp/(?P<object_slug>[^/]+)/edit/$', CustomizedUpdateObjectView.as_view(), { 'extra_context': { 'base_template': 'special_base.html' } } )
I haven't tested this code, so use at your own risk, but it should provide a general guideline on how to get it working correctly.
If you do this, your library's users will be very grateful, even if they don't tell you directly.









