View Django注册:阻止登录(已验证)访问所有用户的注册页面结果到拒绝注册页面
大家好, 我目前正在为Django项目使用Django注册redux 为了防止自动认证用户访问注册页面和登录页面,我需要对服务于这两个页面的views.py进行一些更改。 我曾经View Django注册:阻止登录(已验证)访问所有用户的注册页面结果到拒绝注册页面,view,django-views,django-authentication,django-registration,View,Django Views,Django Authentication,Django Registration,大家好, 我目前正在为Django项目使用Django注册redux 为了防止自动认证用户访问注册页面和登录页面,我需要对服务于这两个页面的views.py进行一些更改。 我曾经 如果self.request.user.u经过身份验证: 返回重定向('mainhomepage') 在下面我最近发布的链接中建议的注册和登录视图 提供的解决方案有效,但它也阻止未登录用户访问注册页面 这是注册页面的views.py class RegistrationView(_RequestPassingFormV
如果self.request.user.u经过身份验证:
返回重定向('mainhomepage')
在下面我最近发布的链接中建议的注册和登录视图
提供的解决方案有效,但它也阻止未登录用户访问注册页面
这是注册页面的views.py
class RegistrationView(_RequestPassingFormView):
"""
Base class for user registration views.
"""
disallowed_url = 'registration_disallowed'
form_class = REGISTRATION_FORM
http_method_names = ['get', 'post', 'head', 'options', 'trace']
success_url = None
template_name = 'registration/registration_form.html'
@method_decorator(sensitive_post_parameters('password1', 'password2'))
def dispatch(self, request, *args, **kwargs):
"""
Check that user signup is allowed before even bothering to
dispatch or do other processing.
"""
if self.request.user.is_authenticated:
return redirect('mainhomepage')
if not self.registration_allowed(request):
return redirect(self.disallowed_url)
return super(RegistrationView, self).dispatch(request, *args, **kwargs)
def form_valid(self, request, form):
new_user = self.register(request, form)
success_url = self.get_success_url(request, new_user)
# success_url may be a simple string, or a tuple providing the
# full argument set for redirect(). Attempting to unpack it
# tells us which one it is.
try:
to, args, kwargs = success_url
return redirect(to, *args, **kwargs)
except ValueError:
return redirect(success_url)
def registration_allowed(self, request):
"""
Override this to enable/disable user registration, either
globally or on a per-request basis.
"""
return True
def register(self, request, form):
"""
Implement user-registration logic here. Access to both the
request and the full cleaned_data of the registration form is
available here.
"""
raise NotImplementedError
如您所见:我添加了
if self.request.user.is_authenticated:
return redirect('mainhomepage')
这样,只有经过身份验证的用户才能重定向到mainhomepageurl
但是,已验证和未验证都会自动指向该页面
class RegistrationView(_RequestPassingFormView):
"""
Base class for user registration views.
"""
disallowed_url = 'registration_disallowed'
form_class = REGISTRATION_FORM
http_method_names = ['get', 'post', 'head', 'options', 'trace']
success_url = None
template_name = 'registration/registration_form.html'
@method_decorator(sensitive_post_parameters('password1', 'password2'))
def dispatch(self, request, *args, **kwargs):
"""
Check that user signup is allowed before even bothering to
dispatch or do other processing.
"""
if self.request.user.is_authenticated:
return redirect('mainhomepage')
if not self.registration_allowed(request):
return redirect(self.disallowed_url)
return super(RegistrationView, self).dispatch(request, *args, **kwargs)
def form_valid(self, request, form):
new_user = self.register(request, form)
success_url = self.get_success_url(request, new_user)
# success_url may be a simple string, or a tuple providing the
# full argument set for redirect(). Attempting to unpack it
# tells us which one it is.
try:
to, args, kwargs = success_url
return redirect(to, *args, **kwargs)
except ValueError:
return redirect(success_url)
def registration_allowed(self, request):
"""
Override this to enable/disable user registration, either
globally or on a per-request basis.
"""
return True
def register(self, request, form):
"""
Implement user-registration logic here. Access to both the
request and the full cleaned_data of the registration form is
available here.
"""
raise NotImplementedError
请问我还能做些什么来解决这个问题。已验证和未验证都会重定向到该页面。同时,if语句只说应该重定向经过身份验证的用户
简而言之,当我注销时,我无法再访问注册页面,因为无论登录状态如何,用户都会被重定向到主页。因此,无法在页面上进行新注册。。。有什么帮助吗???