forked from mirrors/bookwyrm
Shows errors for login/register forms
This commit is contained in:
parent
e67feb9296
commit
33f486e7eb
3 changed files with 50 additions and 10 deletions
|
@ -5,6 +5,9 @@
|
|||
<div class="column">
|
||||
<div class="box">
|
||||
<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">
|
||||
{% csrf_token %}
|
||||
<div class="field">
|
||||
|
@ -12,12 +15,18 @@
|
|||
<div class="control">
|
||||
{{ login_form.username }}
|
||||
</div>
|
||||
{% for error in login_form.username.errors %}
|
||||
<p class="help is-danger">{{ error | escape }}</p>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label" for="id_password">Password:</label>
|
||||
<div class="control">
|
||||
{{ login_form.password }}
|
||||
</div>
|
||||
{% for error in login_form.password.errors %}
|
||||
<p class="help is-danger">{{ error | escape }}</p>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="field is-grouped">
|
||||
<div class="control">
|
||||
|
|
|
@ -2,19 +2,28 @@
|
|||
<div class="field">
|
||||
<label class="label" for="id_username_register">Username:</label>
|
||||
<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>
|
||||
{% for error in register_form.username.errors %}
|
||||
<p class="help is-danger">{{ error | escape }}</p>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label" for="id_email_register">Email address:</label>
|
||||
<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 class="field">
|
||||
<label class="label" for="id_password_register">Password:</label>
|
||||
<div class="control">
|
||||
<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 class="field is-grouped">
|
||||
|
|
|
@ -23,14 +23,15 @@ def user_login(request):
|
|||
if request.method == 'GET':
|
||||
return redirect('/login')
|
||||
|
||||
register_form = forms.RegisterForm()
|
||||
login_form = forms.LoginForm(request.POST)
|
||||
register_form = forms.RegisterForm()
|
||||
if not login_form.is_valid():
|
||||
return TemplateResponse(
|
||||
request,
|
||||
'login.html',
|
||||
{'login_form': login_form, 'register_form': register_form}
|
||||
)
|
||||
data = {
|
||||
'site_settings': models.SiteSettings.get(),
|
||||
'login_form': login_form,
|
||||
'register_form': register_form
|
||||
}
|
||||
return TemplateResponse(request, 'login.html', data)
|
||||
|
||||
username = login_form.data['username']
|
||||
username = '%s@%s' % (username, DOMAIN)
|
||||
|
@ -39,7 +40,14 @@ def user_login(request):
|
|||
if user is not None:
|
||||
login(request, user)
|
||||
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):
|
||||
|
@ -61,13 +69,27 @@ def register(request):
|
|||
invite = None
|
||||
|
||||
form = forms.RegisterForm(request.POST)
|
||||
errors = False
|
||||
if not form.is_valid():
|
||||
return redirect('/register/')
|
||||
errors = True
|
||||
|
||||
username = form.data['username']
|
||||
email = form.data['email']
|
||||
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)
|
||||
if invite:
|
||||
invite.times_used += 1
|
||||
|
|
Loading…
Reference in a new issue