raise ValueError('The given username must be set') using django python
...
I have been trying to register a user using from django.contrib.auth.models import user
Views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth.models import User
# Create your views here.
def register(request):
if request.method == 'POST':
username = request.POST.get('username')
email = request.POST.get('email')
password1 = request.POST.get('password1')
password2 = request.POST.get('password2')
if password1 == password2:
if User.objects.filter(username = username).exists():
return redirect('register')
elif User.objects.filter(email = email).exists():
return redirect('register')
else:
user=User.objects.create_user(username = username,password =password1, email = email)
user.save()
return redirect('login')
else:
return redirect('/')
else:
return render(request, 'register.html')
def login(request):
return render(request, 'login.html')
register.html
<form method="POST" action="{% url 'register' %}">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username">
</div>
<div class="form-group">
<label for="password1">Password</label>
<input type="password" class="form-control" id="password1">
</div>
<div class="form-group">
<label for="password2">AGAIN Password</label>
<input type="password" class="form-control" id="password2">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="name@example.com">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="check">
<label class="form-check-label" for="check">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
user=User.objects.create_superuser(username = username,password =password1, email = email)
I want to know that why it is just sending that The given username is must be set but instead I have set super_user already. How to resolve this? It is not getting the username by POST method or is there any other mistake which I am ignoring?
add name attribute in your input tags, something like this:
...
<input type="text" class="form-control" id="username" name="username">
...
<input type="password" class="form-control" id="password1" name="password1">
...
<input type="password" class="form-control" id="password2" name="password2">
...
<input type="email" class="form-control" id="email" name="email" placeholder="name@example.com">
...