I’m using login/logout functionality built into Django but have 2 issues:
-
I can’t seem to figure out how to change the urls form /accounts/login or logout to appname/login or logout. The templates are in my project directory in templates/registration.
-
My HTML doesn’t display {{ message }} text nor can I center on teh page built in messages such as when an incorrect pw/userid is entered. Templates is commented out because it gives an error:
django.core.exceptions.ImproperlyConfigured: Invalid BACKEND for a template engine: <not defined>. Check your TEMPLATES setting.
'''TEMPLATES = [
{
"DIRS": [BASE_DIR / "templates"], # new
},
]'''
LOGIN_URL = "login"
LOGOUT_URL = "login"
LOGOUT_REDIRECT_URL = "login"
LOGIN_REDIRECT_URL = "/ISO22301/welcome/"
HTML
<!DOCTYPE html>
<html lang = "en">
<head>
{% load static %}
<link rel="stylesheet" href="{% static 'static/css/styles.css' %}">
<title>{{ browsertab }}</title>
<link rel='shortcut icon' type="image/png" href="{% static 'static/images/favicon.png' %}" >
<h1>{{ dashboard_title }}</h1>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
<h1>Welcome to Strategy Mapping</h1>
<h1>Login</h1>
{% for message in messages %}
<center>
<div class='loginmessage'> {{ message }}</div>
<p></p>
{% endfor %}
</center>
<form method='POST' action='{% url 'login' %}' enctype='multipart/form-data'>
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log In</button>
</form>
</body>
urls
urlpatterns = [
#path('login/', views.user_login, name='login'),
path('home/', views.home, name='home'),
path('welcome/', views.welcome, name='welcome'),
path('dashboard/', views.dashboard, name='dashboard'),
path('testpage/', views.testpage, name='testpage'),
#path('logout/', views.user_logout, name='logout_user'),
]
views
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path("ISO22301/", include("ISO22301.urls")),
]
def user_login(request):
form = LoginForm()
context ={
'browsertab': 'Strategia Worldwide',
'form': form,
}
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
current_time = datetime.datetime.now()
return redirect('welcome')
return render(request,'login.html',context)
else:
form = LoginForm()
context ={
'browsertab': 'Strategia Worldwide',
'form': form,
}
return render(request,'login.html',context)
def user_logout(request):
if request.method == 'POST':
if request.user.is_authenticated:
logout(request)
messages.success(request, 'Logout Successful')
return redirect('/accounts/login.html')