Python django自定义注册视图,重定向失败
我正在使用Django注册,我已经将BaseRegistrationView子类化,以定制后端 但是,当满足条件时,HttpResponseRedirect无法重定向到新页面 代码如下所示:Python django自定义注册视图,重定向失败,python,django,http-redirect,django-registration,Python,Django,Http Redirect,Django Registration,我正在使用Django注册,我已经将BaseRegistrationView子类化,以定制后端 但是,当满足条件时,HttpResponseRedirect无法重定向到新页面 代码如下所示: class CustomRegistrationView(BaseRegistrationView): def register(self, request, **cleaned_data): captchaMatch = False #check captcha captch
class CustomRegistrationView(BaseRegistrationView):
def register(self, request, **cleaned_data):
captchaMatch = False
#check captcha
captchaValue = request.session.get('captcha')
captchaValueSubmitted = cleaned_data['captcha'] + "123"
if captchaValue != captchaValueSubmitted:
return HttpResponseRedirect(reverse('InvitationRequired'))
else:
self.captchaMatch = True
username, email, password = cleaned_data['username'], cleaned_data['email'], cleaned_data['password1']
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
def registration_allowed(self, request):
"""
Indicate whether account registration is currently permitted,
based on the value of the setting ``REGISTRATION_OPEN``. This
is determined as follows:
* If ``REGISTRATION_OPEN`` is not specified in settings, or is
set to ``True``, registration is permitted.
* If ``REGISTRATION_OPEN`` is both specified and set to
``False``, registration is not permitted.
"""
return getattr(settings, 'REGISTRATION_OPEN', True)
def get_success_url(self, request, user):
"""
Return the name of the URL to redirect to after successful
user registration.
"""
print "get successful url"
if self.captchaMatch:
return ('registration_complete', (), {})
else:
return HttpResponseRedirect(reverse('InvitationRequired'))
有人能解释一下为什么在函数register中执行重定向吗?我想
register
方法应该返回一个用户或None
。不是HttpRedirect
对象。由于我不熟悉这个特定的应用程序,请尝试查看他们如何处理原始方法上的故障,并遵循该策略(即返回None
或引发异常
)
另外,根据get\u success\u url()
方法上的注释,我希望代码如下所示:
def get_success_url(self, request, user):
"""
Return the name of the URL to redirect to after successful
user registration.
"""
if self.captchaMatch:
return ('registration_complete', (), {})
else:
return ('InvitationRequired', (), {})
我想您不应该返回HttpResponse
实例,因为该方法用于获取要重定向到的URL(而不是重定向本身)