I am working on authentication webapp and I want to display message if user input doesn't match some conditions. It should look something like this.
For example: if password has length less than 8 characters then display message 'Password should be longer than 8 characters!'
My view:
def signup(request):
if request.method == "POST":
context = {'has_error': False,
'data': request.POST,
'length_error': False,
'password_match_error': False,
'validate_email_error': False,
}
global password
global password2
global email
global username
email = request.POST.get('email')
username = request.POST.get('username')
password = request.POST.get('password')
password2 = request.POST.get('password2')
if len(password) < 8:
############custom message
context['length_error'] = True
if password != password2:
############custom message
context['password_match_error'] = True
if not validate_email(email):
############custom message
context['validate_email_error'] = True
if not username:
############custom message
context['has_error'] = True
if models.CustomUser.objects.filter(email=email).exists():
messages.add_message(request, messages.ERROR, 'This email is already registered!')
context['has_error'] = True
return render(request, 'authentication/signup.html', context, status=409)
if context['has_error']:
return render(request, 'authentication/signup.html', context)
body = render_to_string('authentication/email/email_body.html', {
'username': username,
'token': token,
})
send_mail(
"Email Confirmation",
body,
'[email protected]',
[email]
)
return redirect('email-confirmation')
return render(request, 'authentication/signup.html')
My signup.html
{% include "_base.html" %}
{% load static %}
{% block title %}Sign Up{% endblock title %}
{% block content %}
<link rel="stylesheet" href="{% static 'css/authentication/signup.css' %}">
<div >
<form method="post" action="{% url 'signup' %}">
{% csrf_token %}
<input type="text" placeholder="Email" name="email" value="{{ data.email }}">
<input type="text" placeholder="Username" name="username" value="{{ data.username }}">
<input type="text" placeholder="Password" name="password" value="{{ data.password }}">
<input type="text" placeholder="Confirm password" name="password2" value="{{ data.password2 }}">
<button type="submit" >Sign Up</button>
</form>
</div>
{% block scripts %}
<script src="js/signup.js"></script>
{% endblock scripts %}
{% endblock content %}
My signup.js is empty. If you want me to post anything else just comment.
CodePudding user response:
You are passing those error variables into your context so you can just render them in your template, you can do something like this:
{% if length_error %}
<p>Your password must be longer than 8 characters</p>
{% endif %}
But for all of your different errors. Django has a login form inbuilt which validates for this kind of thing, I would suggest using that rather than re-inventing the wheel.
https://docs.djangoproject.com/en/4.0/topics/auth/default/
CodePudding user response:
Displaying messages in template
{% if messages %}
<ul >
{% for message in messages %}
<li{% if message.tags %}
{% endif %}>
{{ message }}</li>
{% endfor %}
</ul>
{% endif %}