I have urls.py :

urlpatterns = [ url(r'^$', views.index, {'check': False}), ] 

There is view.py :

 def index(request, check=False): if check: return HttpResponse("Hello, world. You're at the test index.") else: return HttpResponseRedirect(reverse('autorisation:check_auth')) 

The index method uses the parameter check - whether the user is authorized or not. The idea is this: when you first enter the page, the parameter is False and, based on this, it throws a request for authorization. Authorization should return to the same index method, but with check = True .

Redirect from check_auth method (not working):

 def check_auth(request): return redirect('app_test:index', check=True) 

How to implement this venture? It is advisable not to change views.py - without url parameters.

Mistake:

 NoReverseMatch at /auth/check_auth Reverse for 'index' with arguments '()' and keyword arguments '{'check': True}' not found. 0 pattern(s) tried: [] 

    2 answers 2

    The redirect function you wrote correctly. Another thing is whether url accepts named arguments. If it does not accept, but it is necessary to put named, or transfer already as positional. If nothing is received, then transmit something is useless.

    In general, janga has built-in authentication decorators.

     @login_required def my_view(request): pass 

    Decorator convenient, if the user is not logged in, then it will redirect to the login page. And then return to the page on which he originally wanted.

      And why such a perversion?

       if request.user.is_authenticated(): и дальше по тексту 

      UPD: not used to the local comments, I apologize

       urlpatterns = [ url(r'^$', views.index, {'check': False}, name='index'), url(r'^$', views.index, {'check': True}, name='index_authenticated'), ] def check_auth(request): return redirect('app_test:index_authenticated',) 
      • I have my own authorization logic. Of course, on each page you can check the session and whether its key is active. But I would like to leave this on the side of the view responsible for authorization and simply turning to it by returning the parameter: yes / no. - Hatter
      • Well, since its own logic ... well, url (r '^ $', views.index, {'check': False}, name = 'index'), - Unmind
      • url (r '^ $', views.index, {'check': True}, name = 'index_authenticated'), - Unmind
      • and return redirect ('app_test: index') instead of return redirect ('app_test: index', check = True) - Unmind
      • This option initially occurred to me :) It is possible without a parameter: just send an index from autorisation: check_auth every time and send it from its logic to the authorization page or to another view adjacent to the index. But somehow it is not beautiful, I would like to do with one method. - Hatter