Django redirect user after login
NickName:Zaraki Kenpachi Ask DateTime:2019-09-20T21:24:28

Django redirect user after login

I have a problem when trying to redirect a user after login to the base.html which is in main template folder. Django can't find this template.

I get the error:

 Django tried these URL patterns, in this order:

 1. admin/
 2.
 The current path, base.html, didn't match any of these.

How do I properly set up django to make the redirection work?

Django structure:

accounts
main_folder
    settings.py
    urls.py
staticfiles
templates
    base.html

Short app structure

accounts
    templates
        accounts
            login.html
    urls.py
    views.py

settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

accounts urls.py

from django.urls import path
from .views import*

urlpatterns = [
    path('', login_view),

]

accounts view.py

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib import messages


def login_view(request):
    if request.method == 'POST':
        # get posted data
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        # handle user authentication
        if user is not None and user.is_active and user.is_authenticated:
                login(request, user)
                # move user to main page
                return redirect('base.html')

    return render(request, 'accounts/login.html')

Copyright Notice:Content Author:「Zaraki Kenpachi」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/58029272/django-redirect-user-after-login

Answers
Matt Seymour 2019-09-20T13:30:08

Your redirection is not to a template but to a url. So in your current code on successful login you would be redirected to http://localhost/base.html.\n\nYou will need to change the redirect to a path:\n\nreturn redirect('/some-url/')\n\n\nOr better still make use of named urls.\n\nreturn redirect('some-named-url')\n# this would turn a named url into a url string such as '/auth-url/'\n\n\nYou urls file would look something like: \n\nfrom django.urls import path\nfrom .views import*\n\nurlpatterns = [\n path('', login_view),\n path('auth-url/', <some_view>, name='some-named-url'),\n]\n\n\nThe better more django way.\n\nIf you are not doing anything too extreme you really should look at making use of djangos built-in authentication. Not only is it tried and tested but it also will get patched if a vulnerability was ever discovered.\n\nTo do this you would change the urls to be something like:\n\nfrom django.urls import path\nfrom django.contrib.auth.views import (\n LoginView,\n LogoutView,\n)\n\nurlpatterns = [\n path(\n 'login/',\n LoginView.as_view(template_name='account/login.html'),\n name='login',\n ),\n path(\n 'logged-in/',\n <Your LoginView>,\n name='logged_in',\n ),\n path(\n 'logout/',\n LogoutView.as_view(),\n name='logout',\n ),\n]\n\n\nAnd in your settings file:\n\nLOGIN_URL = 'login' # this is the name of the url\n\nLOGOUT_REDIRECT_URL = 'login' # this is the name of the url\n\nLOGIN_REDIRECT_URL = 'logged_in' # this is the name of the url\n",


More about “Django redirect user after login” related questions

Django redirect user after login

I have a problem when trying to redirect a user after login to the base.html which is in main template folder. Django can't find this template. I get the error: Django tried these URL patterns, in

Show Detail

how to redirect the user to user-specific url after login django?

I am new in django. I am trying to create a user specific page where after login the user land to somthing like mydomain.com/dashboard/. I am trying to implement the following solution Django - after

Show Detail

In Django, after login redirect the user to the previous page

I want to secure each webpage of my django app, and what I want is that after login the user should be redirected to the previous page he was trying to access. So in accounts/views.py this is the l...

Show Detail

Redirect to admin after login - Django

I am trying to redirect the user after login to their appropriate login pages according to their policy. If the user is a staff then after login the user should be redirected to admin page so on. I

Show Detail

Django redirect user after login pending on their group

I am using django's 'login view' via a form on my login.html page : &lt;form class="form-horizontal" method="post" action="{% url 'django.contrib.auth.views.login' %}"&gt;

Show Detail

Django - Login and redirect to user profile page

I am trying to redirect a user who just logged in to his/her's respective account page. This question has been asked a few times, but most of them are old and use static urls like /accounts/profil...

Show Detail

Django Python redirect after login

I know this questions has been answered is one ore another way, but I still cant figure out how to redirect after a user is logged in. I know Django comes with built in websites, but I need a custom

Show Detail

Unable to redirect user to login page after registration using the redirect funtion in django

I am trying to redirect user to login page after registration and i am getting the Reverse for login not found error accounts/urls.py from django.urls import path,include from . import views from

Show Detail

Create User-Specific Redirect After Login in django

I have three types of people in my Django application Admin, Staff and Parent . how can I set up log-in for them as they will have different views after logging in using django registration? my cod...

Show Detail

Conditionaly redirect users after login

I have looked at both Django -- Conditional Login Redirect and Conditional login redirect in Django and neither address my current issue. What I want to do is check a property of the user after they

Show Detail