我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.contrib.auth.views.redirect_to_login()。
def render_flatpage(request, f): """ Internal interface to the flat page view. """ # If registration is required for accessing this page, and the user isn't # logged in, redirect to the login page. if f.registration_required and not request.user.is_authenticated(): from django.contrib.auth.views import redirect_to_login return redirect_to_login(request.path) if f.template_name: template = loader.select_template((f.template_name, DEFAULT_TEMPLATE)) else: template = loader.get_template(DEFAULT_TEMPLATE) # To avoid having to always use the "|safe" filter in flatpage templates, # mark the title and content as already safe (since they are raw HTML # content in the first place). f.title = mark_safe(f.title) f.content = mark_safe(f.content) response = HttpResponse(template.render({'flatpage': f}, request)) return response
def render_flatpage(request, f): """ Internal interface to the flat page view. """ # If registration is required for accessing this page, and the user isn't # logged in, redirect to the login page. if f.registration_required and not request.user.is_authenticated: from django.contrib.auth.views import redirect_to_login return redirect_to_login(request.path) if f.template_name: template = loader.select_template((f.template_name, DEFAULT_TEMPLATE)) else: template = loader.get_template(DEFAULT_TEMPLATE) # To avoid having to always use the "|safe" filter in flatpage templates, # mark the title and content as already safe (since they are raw HTML # content in the first place). f.title = mark_safe(f.title) f.content = mark_safe(f.content) response = HttpResponse(template.render({'flatpage': f}, request)) return response
def is_owner(view_func): @wraps(view_func, assigned=available_attrs(view_func)) def _wrapped_view(request, *args, **kwargs): # assume username is first arg if request.user.is_authenticated(): if request.user.username == kwargs['username']: return view_func(request, *args, **kwargs) protocol = "https" if request.is_secure() else "http" return HttpResponseRedirect("%s://%s" % (protocol, request.get_host())) path = request.build_absolute_uri() login_url = request.build_absolute_uri(settings.LOGIN_URL) # If the login url is the same scheme and net location then just # use the path as the "next" url. login_scheme, login_netloc = urlparse.urlparse(login_url)[:2] current_scheme, current_netloc = urlparse.urlparse(path)[:2] if ((not login_scheme or login_scheme == current_scheme) and (not login_netloc or login_netloc == current_netloc)): path = request.get_full_path() from django.contrib.auth.views import redirect_to_login return redirect_to_login(path, None, REDIRECT_FIELD_NAME) return _wrapped_view
def dispatch(self, request, *args, **kwargs): ''' This override of dispatch ensures that if no group is required, then the request still goes through without being logged in. ''' self.request = request in_group = False required_group = self.get_group_required() if not required_group or required_group == ['']: in_group = True elif self.request.user.is_authenticated(): in_group = self.check_membership(required_group) if not in_group: if self.raise_exception: raise PermissionDenied else: return redirect_to_login( request.get_full_path(), self.get_login_url(), self.get_redirect_field_name()) return super(GroupRequiredMixin, self).dispatch( request, *args, **kwargs)
def user_allowed_for_project(view_func): """ Check that the user is allowed for the project. If the user is not allowed, the view will be redirected to the standard login page. """ @wraps(view_func, assigned=available_attrs(view_func)) def _wrapped_view(request, *args, **kwargs): try: slug = kwargs['slug'] except IndexError: raise ImproperlyConfigured project = get_object_or_404(Project, slug=slug) if project.is_allowed(request.user): return view_func(request, *args, **kwargs) if request.user.is_authenticated(): raise PermissionDenied path = request.build_absolute_uri() return redirect_to_login(path) return _wrapped_view
def cms_perms(func): def inner(request, *args, **kwargs): page = request.current_page if page: if page.login_required and not request.user.is_authenticated(): return redirect_to_login(urlquote(request.get_full_path()), settings.LOGIN_URL) if not page.has_view_permission(request): return _handle_no_page(request, "$") return func(request, *args, **kwargs) inner.__module__ = func.__module__ inner.__doc__ = func.__doc__ if hasattr(func, '__name__'): inner.__name__ = func.__name__ elif hasattr(func, '__class__'): inner.__name__ = func.__class__.__name__ return inner
def render_flatpage(request, f): """ Internal interface to the flat page view. """ # If registration is required for accessing this page, and the user isn't # logged in, redirect to the login page. if f.registration_required and not request.user.is_authenticated(): from django.contrib.auth.views import redirect_to_login return redirect_to_login(request.path) if f.template_name: t = loader.select_template((f.template_name, DEFAULT_TEMPLATE)) else: t = loader.get_template(DEFAULT_TEMPLATE) # To avoid having to always use the "|safe" filter in flatpage templates, # mark the title and content as already safe (since they are raw HTML # content in the first place). f.title = mark_safe(f.title) f.content = mark_safe(f.content) c = RequestContext(request, { 'flatpage': f, }) response = HttpResponse(t.render(c)) return response
def endpoint(self, request, *args, **kwargs): try: return super(PageView, self).endpoint(request, *args, **kwargs) except ApiException as e: if e.error_code == ERROR_CODES.PARAMS: if self.signature_salt: return redirect('/error/invalid_signature/') raise Http404() if e.error_code == ERROR_CODES.SIGNATURE: return redirect('/error/invalid_signature/') if e.error_code == ERROR_CODES.EXPIRED_SIGNATURE: return redirect('/error/expired_link/') if e.status_code != 404: return HttpResponse(e.message, status=e.status_code) if e.error_code != ERROR_CODES.PERMISSION: raise Http404() path = request.get_full_path() return redirect_to_login(path, settings.LOGIN_URL, 'next')
def handle_no_permission(self): if self.raise_exception: raise PermissionDenied(self.get_permission_denied_message()) return redirect_to_login(self.request.get_full_path(), self.get_login_url(), self.get_redirect_field_name())
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): """ Decorator for views that checks that the user passes the given test, redirecting to the log-in page if necessary. The test should be a callable that takes the user object and returns True if the user passes. """ def decorator(view_func): @wraps(view_func, assigned=available_attrs(view_func)) def _wrapped_view(request, *args, **kwargs): if test_func(request.user): return view_func(request, *args, **kwargs) path = request.build_absolute_uri() resolved_login_url = resolve_url(login_url or settings.LOGIN_URL) # If the login url is the same scheme and net location then just # use the path as the "next" url. login_scheme, login_netloc = urlparse(resolved_login_url)[:2] current_scheme, current_netloc = urlparse(path)[:2] if ((not login_scheme or login_scheme == current_scheme) and (not login_netloc or login_netloc == current_netloc)): path = request.get_full_path() from django.contrib.auth.views import redirect_to_login return redirect_to_login( path, resolved_login_url, redirect_field_name) return _wrapped_view return decorator
def redirect_with_next(self, request, redirect_url, redirect_field_name="next"): # ??next???? next = request.build_absolute_uri() login_scheme, login_netloc = urlparse(redirect_url)[:2] current_scheme, current_netloc = urlparse(next)[:2] if ((not login_scheme or login_scheme == current_scheme) and (not login_netloc or login_netloc == current_netloc)): next = request.get_full_path() return redirect_to_login( next, redirect_url, redirect_field_name)
def handle_no_permission(self): if self.raise_exception: raise PermissionDenied(self.get_permission_denied_message()) return redirect_to_login( self.request.get_full_path(), self.get_login_url(), self.get_redirect_field_name() )
def allow_or_deny(self, request): """ Allow the request to continue (return None), redirect to login, or raise PermissionDenied. These weird semantics are so you can do: return allow_or_deny(request) or HttpResponse(...) """ from django.contrib.auth.views import redirect_to_login # Annotate request with some baya information request.baya_requires = self.get_permissions_required_data(request) # Set BAYA_ALLOW_ALL while testing in development to disable # permissions checking. if getattr(settings, 'BAYA_ALLOW_ALL', False): return None # Check permissions before checking if the user is authenticated, # to allow views to be protected by empty RolesNodes to be served. if (request.method in self.POST_METHODS and self.has_post_permission(request)): return None elif (request.method in self.GET_METHODS and self.has_get_permission(request)): return None is_authenticated = ( request.user.is_authenticated() if django.VERSION[:2] < (1, 10) else request.user.is_authenticated) if not is_authenticated: path = request.get_full_path() return redirect_to_login(path, self.login_url) permission_denied_msg = ( "User {user} does not have permission to {method} to this " "resource. Groups {groups} are required, but {user} only has " "{user_groups}").format( user=request.user, method=request.method, groups=request.baya_requires['requires_groups'], user_groups=request.baya_requires['user_groups']) raise PermissionDenied(permission_denied_msg)
def process_request(self, request): for prefix in NO_LOGIN_REQUIRED_PREFIXES: if request.path.startswith(prefix): return None for prefix in LOGIN_REQUIRED_PREFIXES: if request.path.startswith(prefix) and \ not request.user.is_authenticated(): from django.contrib.auth.views import redirect_to_login return redirect_to_login(request.get_full_path()) return None