N
N
nurzhannogerbek2017-09-13 15:26:20
Django
nurzhannogerbek, 2017-09-13 15:26:20

Django sees the default redirect address. How to fix?

Hello! Please help me fix a very strange problem.
In my Django project, I do not use the admin panel out of the box, but created my own, custom one. I'm using the authorization from django.contrib.auth for the admin login page. At the bottom you can see the urls.py file where the next parameter was specified. Checked, everything works. After successful authorization, the administrator is redirected to the address specified in next ( /administration/dashboard/).
Wrote a unit test . Throws an error on assertRedirects . From the error, you can understand that Django issues a redirect address, which is usually the default ( /accounts/profile/). Why can't the unit test see the settings in urls.py?
The problem disappears only if you add a similar entry to the settings LOGIN_REDIRECT_URL = '/administration/dashboard/'. I cannot use this entry, since LOGIN_REDIRECT_URL will be used for the authorization page for ordinary users in the future.
How to solve this strange problem?
URLs.py:

from django.contrib.auth import views as authentication_views

urlpatterns = [
    # Administration Login
    url(r'^login/$',
        authentication_views.login,
        {
            'template_name': 'administration/login.html',
            'authentication_form': AdministrationAuthenticationForm,
            'extra_context': {
                'next': reverse_lazy('administration:dashboard'),
            },
            'redirect_authenticated_user': True
        },
        name='administration_login'),
]

tests.py:
class AdministrationViewTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        self.credentials = {'username': 'user', 'password': 'password'}
        self.user = User.objects.create_user(**self.credentials)

    def test_administration_authorization(self):
        self.assertTrue(self.user)

        logged_in = self.client.login(**self.credentials)
        self.assertTrue(logged_in)

        response = self.client.post(
            reverse("administration:administration_login"),
            follow_redirects=True
        )
        self.assertEqual(response.status_code, 302)
        self.assertRedirects(
            response,
            reverse("administration:dashboard"),
            status_code=302,
            target_status_code=200
        )

ERROR:
Traceback (most recent call last):
  File "/home/nurzhan/CA/administration/tests.py", line 58, in test_administration_authorization
    target_status_code=200
  File "/srv/envs/py27/lib/python2.7/site-packages/django/test/testcases.py", line 324, in assertRedirects
    % (path, redirect_response.status_code, target_status_code)
AssertionError: Couldn't retrieve redirection page '/accounts/profile/': response code was 404 (expected 200)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question