I am watching YouTube course which is named "Python Backend Web Development Course (with Django)" and I ran into some problems here. I wrote the same code as in video but my registration form does not work.
When I press Submit button no error-messages pops up and user does not register.
Can you help me to solve this problem please?
views.py
def register(request):
context = {}
if request.method == 'POST':
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email is already used')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'Username is already used')
return redirect('register')
else:
user = User.objects.create_user(
username=username, email=email, password=password)
user.save();
return redirect('login')
else:
messages.info(request, 'Password does not match')
return redirect('register')
else:
return render(request, 'register.html')
register.html
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Sign up</h1>
<style>
h5 {
color: red;
}
</style>
{% for message in messages %}
<h5>{{message}}</h5>
{% endfor %}
<form mehod="POST" action="register">
{% csrf_token %}
<p>Username:</p>
<input type="text" name="username" />
<p>Email:</p>
<input type="email" name="email" />
<p>Password:</p>
<input type="password" name="password" />
<p>Confirm password:</p>
<input type="password" name="password2" /><br />
<input type="submit" />
</form>
</body>
</html>
Any advise will help because I do really want to figure out where is the problem and why my code does not work properly.
CodePudding user response:
Try This:
def register(request):
context = {}
if request.method == 'POST':
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email is already used')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'Username is already used')
return redirect('register')
else:
user = User(
username=username,
email=email,
password=password) # i changed objects.create_user() to User()
user.save();
return redirect('login')
else:
messages.info(request, 'Password does not match')
return redirect('register')
else:
return render(request, 'register.html')
CodePudding user response:
So the problem was in mispelling, sorry everyone for posting this question. I should've checked it properly