Я написал такой код в views:
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
from django.http import HttpResponse
from django.contrib import messages
# Create you're views here.
def index(request):
return render(request, 'eshkere.html')
@login_required
def loginedpage(request):
return HttpResponse("LOL")
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
return redirect("/lolkek")
else:
# Return an 'invalid login' error message.
messages.add_message(request, messages.INFO, 'Hello world.')
return render(request, 'eshkere.html')
ДА, гавно но это чисто для практики. Вот eshkere.html:
<form action="/j" method="POST">
{% csrf_token %}
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Submit">
{% if messages %}
{{ messages }}
{% endif %}
</form>
У меня вылазит на странице нормальная форма, но с сообщением когда надо:
В settings.py:
MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage'
INSTALLED_APPS = [
'kek',
]