基于 Django Class 的视图 get_context 未找到获取请求
Django Class based view get_context not finding get request
我正在编辑 django allauth 以在设备移动时更改模板,我已经尝试使用 django-mobile
request.mobile
和 request.GET.get('mobile')
都不起作用url 是 /accounts/signup/?mobile=true
这是我目前得到的
class LoginView(RedirectAuthenticatedUserMixin,
AjaxCapableProcessFormViewMixin,
FormView):
form_class = LoginForm
template_name = "account/login.html"
success_url = None
redirect_field_name = "next"
def form_valid(self, form):
success_url = self.get_success_url()
return form.login(self.request, redirect_url=success_url)
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 get_context_data(self, **kwargs):
ret = super(LoginView, self).get_context_data(**kwargs)
signup_url = passthrough_next_redirect_url(self.request,
reverse("account_signup"),
self.redirect_field_name)
redirect_field_value = self.request.REQUEST \
.get(self.redirect_field_name)
if self.request.GET.get('mobile'):
self.template_name = 'mobile/sign_up.html'
ret.update({"signup_url": signup_url,
"site": Site.objects.get_current(),
"redirect_field_name": self.redirect_field_name,
"redirect_field_value": redirect_field_value})
return ret
这里有多余的 space 符号:
redirect_field_value = self.request.REQUEST \
.get(self.redirect_field_name) # ^ space should be removed!
如果您重写 .get_template_names(…)
而不是 .get_context_data(…)
,您将获得更清晰的解决方案,并有机会在解析的精确点替换模板名称。
我正在编辑 django allauth 以在设备移动时更改模板,我已经尝试使用 django-mobile
request.mobile
和 request.GET.get('mobile')
都不起作用url 是 /accounts/signup/?mobile=true
这是我目前得到的
class LoginView(RedirectAuthenticatedUserMixin,
AjaxCapableProcessFormViewMixin,
FormView):
form_class = LoginForm
template_name = "account/login.html"
success_url = None
redirect_field_name = "next"
def form_valid(self, form):
success_url = self.get_success_url()
return form.login(self.request, redirect_url=success_url)
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 get_context_data(self, **kwargs):
ret = super(LoginView, self).get_context_data(**kwargs)
signup_url = passthrough_next_redirect_url(self.request,
reverse("account_signup"),
self.redirect_field_name)
redirect_field_value = self.request.REQUEST \
.get(self.redirect_field_name)
if self.request.GET.get('mobile'):
self.template_name = 'mobile/sign_up.html'
ret.update({"signup_url": signup_url,
"site": Site.objects.get_current(),
"redirect_field_name": self.redirect_field_name,
"redirect_field_value": redirect_field_value})
return ret
这里有多余的 space 符号:
redirect_field_value = self.request.REQUEST \
.get(self.redirect_field_name) # ^ space should be removed!
如果您重写 .get_template_names(…)
而不是 .get_context_data(…)
,您将获得更清晰的解决方案,并有机会在解析的精确点替换模板名称。