Shows errors for login/register forms

This commit is contained in:
Mouse Reeve 2020-10-04 15:10:34 -07:00
parent e67feb9296
commit 33f486e7eb
3 changed files with 50 additions and 10 deletions

View file

@ -5,6 +5,9 @@
<div class="column"> <div class="column">
<div class="box"> <div class="box">
<h2 class="title">Log in</h2> <h2 class="title">Log in</h2>
{% if login_form.non_field_errors %}
<p class="notification is-danger">{{ login_form.non_field_errors }}</p>
{% endif %}
<form name="login" method="post" action="/user-login"> <form name="login" method="post" action="/user-login">
{% csrf_token %} {% csrf_token %}
<div class="field"> <div class="field">
@ -12,12 +15,18 @@
<div class="control"> <div class="control">
{{ login_form.username }} {{ login_form.username }}
</div> </div>
{% for error in login_form.username.errors %}
<p class="help is-danger">{{ error | escape }}</p>
{% endfor %}
</div> </div>
<div class="field"> <div class="field">
<label class="label" for="id_password">Password:</label> <label class="label" for="id_password">Password:</label>
<div class="control"> <div class="control">
{{ login_form.password }} {{ login_form.password }}
</div> </div>
{% for error in login_form.password.errors %}
<p class="help is-danger">{{ error | escape }}</p>
{% endfor %}
</div> </div>
<div class="field is-grouped"> <div class="field is-grouped">
<div class="control"> <div class="control">

View file

@ -2,19 +2,28 @@
<div class="field"> <div class="field">
<label class="label" for="id_username_register">Username:</label> <label class="label" for="id_username_register">Username:</label>
<div class="control"> <div class="control">
<input type="text" name="username" maxlength="150" class="input" required="" id="id_username_register"> <input type="text" name="username" maxlength="150" class="input" required="" id="id_username_register" value="{% if register_form.username.value %}{{ register_form.username.value }} {% endif %}">
</div> </div>
{% for error in register_form.username.errors %}
<p class="help is-danger">{{ error | escape }}</p>
{% endfor %}
</div> </div>
<div class="field"> <div class="field">
<label class="label" for="id_email_register">Email address:</label> <label class="label" for="id_email_register">Email address:</label>
<div class="control"> <div class="control">
<input type="email" name="email" maxlength="254" class="input" id="id_email_register"> <input type="email" name="email" maxlength="254" class="input" id="id_email_register" value="{% if register_form.email.value %}{{ register_form.email.value }}{% endif %}">
{% for error in register_form.email.errors %}
<p class="help is-danger">{{ error | escape }}</p>
{% endfor %}
</div> </div>
</div> </div>
<div class="field"> <div class="field">
<label class="label" for="id_password_register">Password:</label> <label class="label" for="id_password_register">Password:</label>
<div class="control"> <div class="control">
<input type="password" name="password" maxlength="128" class="input" required="" id="id_password_register"> <input type="password" name="password" maxlength="128" class="input" required="" id="id_password_register">
{% for error in register_form.password.errors %}
<p class="help is-danger">{{ error | escape }}</p>
{% endfor %}
</div> </div>
</div> </div>
<div class="field is-grouped"> <div class="field is-grouped">

View file

@ -23,14 +23,15 @@ def user_login(request):
if request.method == 'GET': if request.method == 'GET':
return redirect('/login') return redirect('/login')
register_form = forms.RegisterForm()
login_form = forms.LoginForm(request.POST) login_form = forms.LoginForm(request.POST)
register_form = forms.RegisterForm()
if not login_form.is_valid(): if not login_form.is_valid():
return TemplateResponse( data = {
request, 'site_settings': models.SiteSettings.get(),
'login.html', 'login_form': login_form,
{'login_form': login_form, 'register_form': register_form} 'register_form': register_form
) }
return TemplateResponse(request, 'login.html', data)
username = login_form.data['username'] username = login_form.data['username']
username = '%s@%s' % (username, DOMAIN) username = '%s@%s' % (username, DOMAIN)
@ -39,7 +40,14 @@ def user_login(request):
if user is not None: if user is not None:
login(request, user) login(request, user)
return redirect(request.GET.get('next', '/')) return redirect(request.GET.get('next', '/'))
return redirect('/login')
login_form.non_field_errors = 'Username or password are incorrect'
data = {
'site_settings': models.SiteSettings.get(),
'login_form': login_form,
'register_form': register_form
}
return TemplateResponse(request, 'login.html', data)
def register(request): def register(request):
@ -61,13 +69,27 @@ def register(request):
invite = None invite = None
form = forms.RegisterForm(request.POST) form = forms.RegisterForm(request.POST)
errors = False
if not form.is_valid(): if not form.is_valid():
return redirect('/register/') errors = True
username = form.data['username'] username = form.data['username']
email = form.data['email'] email = form.data['email']
password = form.data['password'] password = form.data['password']
# check username and email uniqueness
if models.User.objects.filter(localname=username).first():
form.add_error('username', 'User with this username already exists')
errors = True
if errors:
data = {
'site_settings': models.SiteSettings.get(),
'login_form': forms.LoginForm(),
'register_form': form
}
return TemplateResponse(request, 'login.html', data)
user = models.User.objects.create_user(username, email, password) user = models.User.objects.create_user(username, email, password)
if invite: if invite:
invite.times_used += 1 invite.times_used += 1