将参数传递给Django/Allauth中的覆盖视图 [英] Passing Parameters to An Overrode View in Django/Allauth

查看:128
本文介绍了将参数传递给Django/Allauth中的覆盖视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Django 1.4和Allauth,我试图拥有2个不同的注册页面.当我说不同时,我的意思是2个不同的URL和"html布局".

With Django 1.4 and Allauth, I'm trying to have 2 different signup pages. When I say different, I mean 2 different URLs and 'html layouts'.

这是我到目前为止所做的.它可以正常运行,但不会将is_alternate_template变量传递给HTML模板:

Here's what I did so far. It 'works', but it doesn't pass the 'is_alternate_template' variable to the HTML template:

在urls.py中:

url(r'^accounts/', include('allauth.urls')),
url(r'^abc/alternate-signup/?$','project.views.alternate_signup'),


在views.py中:


in views.py:

def alternate_signup(request):
    from allauth.account import views as account_views
    is_alternate_template = True
    return account_views.signup(request, locals(), context_instance=RequestContext(request))


在templates/account/signup.html


in templates/account/signup.html

{% if is_alternate_template %}
display the alternate layout of the signup page
{% else %}
display the 'standard' layout of the signup page
{% endif %}


在Allauth模块中,这是帐户/视图中注册视图的外观(这是我从Allauth模块覆盖的视图,而不是我自己写的视图):


In the Allauth module, here's what the signup view looks like in account/views (this is the view I overrode from the Allauth module, not something I wrote myself):

class SignupView(RedirectAuthenticatedUserMixin, CloseableSignupMixin, FormView):
    template_name = "account/signup.html"
    form_class = SignupForm
    redirect_field_name = "next"
    success_url = None

    def get_success_url(self):
        # Explicitly passed ?next= URL takes precedence
        ret = (get_next_redirect_url(self.request,
                                     self.redirect_field_name)
               or self.success_url)
        return ret

    def form_valid(self, form):
        user = form.save(self.request)
        return complete_signup(self.request, user,
                               app_settings.EMAIL_VERIFICATION,
                               self.get_success_url())

    def get_context_data(self, **kwargs):
        form = kwargs['form']
        form.fields["email"].initial = self.request.session.get('account_verified_email', None)
        ret = super(SignupView, self).get_context_data(**kwargs)
        login_url = passthrough_next_redirect_url(self.request,
                                                  reverse("account_login"),
                                                  self.redirect_field_name)
        redirect_field_name = self.redirect_field_name
        redirect_field_value = self.request.REQUEST.get(redirect_field_name)
        ret.update({"login_url": login_url,
                    "redirect_field_name": redirect_field_name,
                    "redirect_field_value": redirect_field_value })
        return ret

signup = SignupView.as_view()

推荐答案

您需要做的是在您的views.py中创建SignupView的子类,这真的很简单.

What your need to do is to create a subclass of SignupView in your views.py it can be really simple.

class SignupViewExt(SignupView):

    template_name = "account/signup_alternate.html"

是的,就这么简单,因为只需更改模板即可.然后将您的urls.py更改为

Yes, as simple as that because the template is the only thing you need to change. Then change your urls.py as

url(r'^abc/alternate-signup/?$',project.views.SignupViewExt.as_view()),

如果要传递其他参数:

url(r'^abc/alternate-signup/?$',project.views.SignupViewExt.as_view(param='some value')),

然后可以作为self.param供SignupViewExt使用.

that would then be available to SignupViewExt as self.param.

这篇关于将参数传递给Django/Allauth中的覆盖视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆