Answer the question
In order to leave comments, you need to log in
Integrity error: column username is not unique. How to fix?
There are registration and login forms. After entering the data and clicking on the button, an error appears: "Integrity error: column username is not unique". But I only have email, I don't use username. What's wrong, how to fix it?
views:
def register(request):
registered = False
if request.method == "POST":
user_form = UserForm(data=request.POST)
if user_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
registered = True
else:
print(user_form.errors)
else:
user_form = UserForm()
return render(request, 'landing.html', {'user_form':user_form, 'registered':registered})
def user_login(request):
if request.method == "POST":
email = request.POST.get('email')
password = request.POST.get('password')
user = authenticate(email='email', password='password')
if user:
if user.is_active():
login(request, user)
return HttpResponseRedirect(reverse('materials'))
else:
return HttpResponse("Account is not active")
else:
return HttpResponse("Failed to login")
else:
return render(request, 'landing.html', {})
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ('email', 'password')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question