简体   繁体   English

具有不同模板和参数的基于Django注册的类的视图

[英]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. django-registration将extra_context传递到“注册表”之后 ,我已经能够向django-registration注册表发送额外的上下文,但不能发送给django-registration的其他页面,例如registration_success页面和activation_complete页面。

All I want to do is pass one parameter to each of these django-registration to tell them how to display. 我要做的就是将一个参数传递给每个django-registration,以告诉他们如何显示。 But how to do this does not seem clear to me. 但是如何做到这一点对我来说似乎还不清楚。

At the moment this is part of my urls.py : 目前,这是我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: 因此,例如,注册/完成页面使用基于OneBoxView的分类视图,如下所示:

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. 但是此视图功能只有一个模板,我找不到如何获取单个django注册页面将模板传递给类的方法。 Setting up a class-based view means that the source code ( direct_to_template, {'template': 'registration/registration_complete.html'}, ) fails to work. 设置基于类的视图意味着源代码( direct_to_template, {'template': 'registration/registration_complete.html'}, )无法正常工作。

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. 我不想为每个URL编写单独的视图函数,并且编写一个大的'if'函数或% self.kwargs['template']来获取页面名称似乎也% self.kwargs['template'] There must be some elegant way for most of the pages of django-registration to simply be passed a "design_onebox" parameter? 对于django-registration的大多数页面,必须有某种简单的方法来简单地传递“ design_onebox”参数?

Variable template name can be passed to a TemplateView class in two ways: 可以通过两种方式将变量模板名称传递给TemplateView类:

  • redefining get_template_names method 重新定义get_template_names方法

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. 如果render_to_response被覆盖,则可能不会被调用。

  • passing template_name to as_view method: 将template_name传递给as_view方法:
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) 最后一个选择是覆盖RegistrationView类的get_success_url方法,默​​认情况下该方法很简单(忽略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. 但是从您的问题中,我无法猜测必须从template_name传递到哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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