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