Passing Parameters to An Overrode View in Django/Allauth

放肆的年华 提交于 2019-12-11 03:54:05

问题


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'.

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


In urls.py:

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

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))

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 %}

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()

回答1:


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"

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()),

if you want to pass additional parameters:

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

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



来源:https://stackoverflow.com/questions/33295796/passing-parameters-to-an-overrode-view-in-django-allauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!