avoiding /accounts/login/?next in django

Viewed 2708

urls.py

#...
from myapp.views import MyView
from django.contrib.auth.decorators import login_required


urlpatterns = [
    #....
    url(r'^terminator/', login_required(MyView.as_view()), name='sexy')
    
]

views.py

class MyView(View):
    
    
    def get(self, request):
        return render(request, 'itworks.html')
    

I am accessing MyView class via Somelogin class which redirects to "terminator url". But the problem is: django redirects me to following url http://127.0.0.1:8000/accounts/login/?next=/terminator.

Of course, this address is not defined and gives me 404.

I played with LOGIN_REDIRECT_URL in settings, but this just brings more confusion to code. So is there anyway to avoid this "default/next" in django and just go to http://127.0.0.1:8000/terminator.

2 Answers

You need to define LOGIN_URL in your settings.py. That will override the default URL of accounts/login.

Add to your settings:

LOGIN_URL = 'login'

Before:

http://localhost:8000/accounts/login/?next=/password/reset

After:

http://localhost:8000/login?next=/password/reset

You can avoid it by going to admin and logging in there,so that you wont be redirected. I resolved this issue by adding: url(r'^accounts/',include(admin.site.urls)) in urls.py file. Hope it helps

Related