I am encountering an error when trying to register a user in my database. The user table I am using is not the default Django user model.
The error is:
the error is in a diferent language because this text is translated for the english. But the code is right.
Models:
class User(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(unique=True)
password = models.CharField(max_length=50)
creation_date = models.DateTimeField(auto_now_add=True)
admin = models.BooleanField(default=False)
def __str__(self):
return self.name
Views:
def register(request):
if request.method == 'POST':
form = request.POST
if form is not None:
if User.objects.filter(email=form.get('email')).exists():
return render(request, 'register.html', {'error': 'Email already in use'})
new_user = User.objects.create(name=form.get('name'), email=form.get('email'), password=form.get('password'), creation_date=date.today(), admin=False)
new_user.save()
return redirect('login')
else:
return render(request, 'register.html')
HTML
<form method='POST' action="{% url 'register' %}">
{% csrf_token %}
<p>Create your account</p>
<div data-mdb-input-init class="form-outline mb-4">
<input type="text" class='form-control' id='name' name="name" placeholder="Name" pattern="^[a-zA-Z][a-zA-Z-_\.]{3,20}$" required>
<label class="form-label" for="name">Name</label>
</div>
<div data-mdb-input-init class="form-outline mb-4">
<span class='text-danger' id='msgEmail'></span>
<input type="email" id="email" class="form-control" placeholder="Email" name="email" />
<label class="form-label" for="email">Email</label>
</div>
<div data-mdb-input-init class="form-outline mb-4">
<span class='text-danger' id='message'></span>
<input type="password" id="password" class="form-control" name="password" />
<label class="form-label" for="password">Password</label>
</div>
<div data-mdb-input-init class="form-outline mb-4">
<span class='text-danger' id='messagePassword2'></span>
<input type="password" id="confirmPassword" class="form-control" name="confirmPassword" />
<label class="form-label" for="confirmPassword">Confirm Password</label>
</div>
<div class="text-center pt-1 mb-5 pb-1">
<button id='button' data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-block fa-lg gradient-custom-2 mb-3" type="submit">Register</button>
</div>
</form>