将参数传递给 Django/Allauth 中的覆盖视图
Passing Parameters to An Overrode View in Django/Allauth
使用 Django 1.4 和 Allauth,我尝试使用 2 个不同的注册页面。当我说不同时,我指的是 2 个不同的 URL 和 'html layouts'.
这是我目前所做的。它 'works',但它不会将 'is_alternate_template' 变量传递给 HTML 模板:
在urls.py中:
url(r'^accounts/', include('allauth.urls')),
url(r'^abc/alternate-signup/?$','project.views.alternate_signup'),
在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
{% if is_alternate_template %}
display the alternate layout of the signup page
{% else %}
display the 'standard' layout of the signup page
{% endif %}
在 Allauth 模块中,account/views 中的注册视图如下所示(这是我从 Allauth 模块覆盖的视图,不是我自己写的):
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 的子类,它可以非常简单。
class SignupViewExt(SignupView):
template_name = "account/signup_alternate.html"
是的,就这么简单,因为模板是您唯一需要更改的东西。然后将您的 urls.py 更改为
url(r'^abc/alternate-signup/?$',project.views.SignupViewExt.as_view()),
如果你想传递额外的参数:
url(r'^abc/alternate-signup/?$',project.views.SignupViewExt.as_view(param='some value')),
然后 SignupViewExt 可以使用 self.param。
使用 Django 1.4 和 Allauth,我尝试使用 2 个不同的注册页面。当我说不同时,我指的是 2 个不同的 URL 和 'html layouts'.
这是我目前所做的。它 'works',但它不会将 'is_alternate_template' 变量传递给 HTML 模板:
在urls.py中:
url(r'^accounts/', include('allauth.urls')),
url(r'^abc/alternate-signup/?$','project.views.alternate_signup'),
在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
{% if is_alternate_template %}
display the alternate layout of the signup page
{% else %}
display the 'standard' layout of the signup page
{% endif %}
在 Allauth 模块中,account/views 中的注册视图如下所示(这是我从 Allauth 模块覆盖的视图,不是我自己写的):
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 的子类,它可以非常简单。
class SignupViewExt(SignupView):
template_name = "account/signup_alternate.html"
是的,就这么简单,因为模板是您唯一需要更改的东西。然后将您的 urls.py 更改为
url(r'^abc/alternate-signup/?$',project.views.SignupViewExt.as_view()),
如果你想传递额外的参数:
url(r'^abc/alternate-signup/?$',project.views.SignupViewExt.as_view(param='some value')),
然后 SignupViewExt 可以使用 self.param。