简体   繁体   中英

Django-registration class-based views with different templates and parameters

Following django-registration passing extra_context to Registration Form , I have been able to send extra contexts to the django-registration Registration Form but not to the other pages of django-registration, like the registration_success page and the activation_complete page.

All I want to do is pass one parameter to each of these django-registration to tell them how to display. But how to do this does not seem clear to me.

At the moment this is part of my urls.py :

(r'^accounts/login/$', 'django.contrib.auth.views.login', { 'extra_context' :     {'design_form': True }}),
(r'^accounts/register/complete/$', OneBoxView.as_view()),
(r'^accounts/register/$', MyRegistrationView.as_view(form_class=RegistrationForm)),
(r'^accounts/', include('registration.backends.default.urls')),

So, for instance, the register/complete page uses the OneBoxView classed based view, which looks like this:

class OneBoxView(TemplateView):
    template_name = 'registration/registration_complete.html'
    def get_context_data(self, **kwargs):
            context = super(OneBoxView, self).get_context_data(**kwargs)
            context.update({
                'design_onebox': True,
            })

But this view function has a single template, and I can't find how to get the individual django-registration pages to pass a template to the class. Setting up a class-based view means that the source code ( direct_to_template, {'template': 'registration/registration_complete.html'}, ) fails to work.

I don't want to write separate view functions for each of the urls, and writing a big 'if' function or % self.kwargs['template'] to grab the name of the page seems to be inelegant too. There must be some elegant way for most of the pages of django-registration to simply be passed a "design_onebox" parameter?

Variable template name can be passed to a TemplateView class in two ways:

  • redefining get_template_names method

Returns a list of template names to be used for the request. Must return a list. May not be called if render_to_response is overridden.

  • passing template_name to as_view method:
TemplateView.as_view(template_name='some_template_name')

Last option is to overwrite get_success_url method of RegistrationView class, which by default is simply (omitting docstring)

def get_success_url(self, request, user):
    return ('registration_complete', (), {})

But from your question I can't guess where from template_name have to be passed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM