22π
β
in your template
{% for message in messages %}
<div class="alert alert-success">
<a class="close" href="#" data-dismiss="alert">Γ</a>
{{ message }}
</div>
{% endfor %}
in view
from django.contrib import messages
def login(request):
if request.method == 'POST':
form = AuthenticationForm(request.POST)
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth_login(request, user)
return redirect('index')
else:
messages.error(request,'username or password not correct')
return redirect('login')
else:
form = AuthenticationForm()
return render(request, 'todo/login.html', {'form': form})
π€Exprator
20π
You should just add inside your template:
{% block title %}Login{% endblock %}
{% block content %}
<h2>Login</h2>
{% if form.errors %}
<p>username or password not correct</p>
{% endif %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endblock %}
π€Mike
4π
Updating for Django 2.0:
For individual field errors (e.g) :
<input type="text" class="form-control my-4" id="id_username" placeholder="Username" name="username">
{% for error in form.username.errors %}
<p class="text-danger">{{ error }}</p>
{% endfor %}
For non field errors (e.g if password is incorrect):
{% if form.non_field_errors %}
{% for error in form.non_field_errors %}
<p class="text-danger">{{ error }}</p>
{% endfor %}
{% endif %}
You should place the above at the end of all fields.
- Django reverse to contains/icontains
- Django Rest Framework β AssertionError Fix your URL conf, or set the `.lookup_field` attribute on the view correctly
- Django Rest Framework Nested Serializers
- Django crontab not executing test function
- How to Model a Foreign Key in a Reusable Django App?
1π
You already have {{ form.as_p }}
in your template, so Django will show any form errors.
The problem is that the AuthenticationForm
takes request
as its first argument. You can then use form.is_valid()
to check whether the username and password are valid and that the user is active, instead of fetching the data from request.POST
manually.
def login(request):
if request.method == 'POST':
form = AuthenticationForm(request, request.POST)
if form.is_valid():
auth_login(self.request, form.get_user())
return redirect('index')
else:
form = AuthenticationForm(request)
return render(request, 'todo/login.html', {'form': form})
Another option would be to use the built-in LoginView
instead of writing your own:
from django.contrib.auth import views as auth_views
urlpatterns = [
...
path('accounts/login/', auth_views.LoginView.as_view(template_name='todo/login.html')),
...
]
Then set LOGIN_REDIRECT_URL
in your settings.
LOGIN_REDIRECT_URL = 'index'
π€Alasdair
- Django SSL redirection on Heroku: 'Too many redirects'
- Django: ValueError: Lookup failed for model referenced by field account.UserProfile.user: auth.User
- Displaying Django Messages Framework Messages
Source:stackexchange.com