#23050 closed New feature (worksforme)

Context Dictionary

Reported by: petkostas@… Owned by: nobody
Component: Generic views Version: 1.7-rc-1
Severity: Normal Keywords: context, Class Based Views
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It would be nice to add a class attribute that holds small data instead of overriding everytime the get_context_data for small bits of elements.
To make this more clear, think of a Form which is handled by a CreateView, for DRY I would use a single add.html template (instead of repeating the template for each app), now since there are a lot of developers out there that use a custom type of admin interface, it would be nice if we could pass some data to the template without having to override the get_context_data, for example:

from django.views import generic

class PageCreateView(generic.CreateView):
    model = Page
    template_name = "myproject/forms/add.html"

    def get_context_data(self, *args, **kwargs):
        context = super(PageCreateView, self).get_context_data(*args, **kwargs)
        context['header'] = 'Add a new page'
        return context

Instead we could:

from django.views import generic

class PageCreateView(generic.CreateView):
    model = Page
    exta_context = {"header":"Add a new page"}
    template_name = "myproject/forms/add.html"

Change History (1)

comment:1 Changed 10 months ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

You can :-) From Django 1.5, the context contains a 'view' key pointing to the current view instance. I think this does the job.

Note: See TracTickets for help on using tickets.
Back to Top