Answer the question
In order to leave comments, you need to log in
How to redirect an unauthorized user to a specific page?
Hello! Please help me figure it out and point me to the right path.
How to redirect an unauthorized user to a specific page?
For example, an unauthorized user visits /admin/ , you need to redirect this user to /admin/login/ . In cases where he is authorized, he calmly goes to the address /admin/ .
PS The task was to create my own admin panel without using the one that is out of the box in Django. Below, I created a custom form for the login page, which inherits from AuthenticationForm.
URLs.py:
urlpatterns = [
# Admin Dashboard
url(r'^admin/$', DashboardView.as_view()),
# Admin Login
url(r'^admin/login/$',
authentication_views.login,
{
'template_name': 'administration/login.html',
'authentication_form': AdminAuthenticationForm
},
name='login'),
]
class AdminAuthenticationForm(AuthenticationForm):
"""
A custom authentication form used in the administration app.
"""
error_messages = {
'invalid_login': ("Ошибка! Проверьте ваш Логин или Пароль администратора еще раз."),
}
required_css_class = 'required'
def confirm_login_allowed(self, user):
if not user.is_active or not user.is_staff:
raise forms.ValidationError(
self.error_messages['invalid_login'],
code='invalid_login',
params={'username': self.username_field.verbose_name}
)
Answer the question
In order to leave comments, you need to log in
Simply mark the desired view with the @login_required decorator and either specify the login_url in the decorator's parameters, or specify the LOGIN_URL parameter in the settings .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question