Django- ValueError [The User couldn't be created because the data didn't validate]
I used extend user model based on this tutorial.When I submit the registration form this error occurs.This errors shows for both time I use is_active() and is_active in my code.
How to resolve this error?
Error
registration form
new error
Here is my code:
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=256, blank=False)
codeforces_id = models.CharField(max_length=256, blank=False)
Uva_Id = models.CharField(max_length=256, blank=False)
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm
class UserFormView(View):
user_form_class = UserForm
profile_form_class= ProfileForm
#display a blank form
def get(self , request):
user_form = self.user_form_class(None)
profile_form = self.profile_form_class(None)
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
#process form data
def post(self, request):
user_form = self.user_form_class(request.POST)
profile_form = self.profile_form_class(request.POST)
user= user_form.save(commit= False)
if user_form.is_valid() and profile_form.is_valid():
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user_form.save()
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
python django
add a comment |
I used extend user model based on this tutorial.When I submit the registration form this error occurs.This errors shows for both time I use is_active() and is_active in my code.
How to resolve this error?
Error
registration form
new error
Here is my code:
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=256, blank=False)
codeforces_id = models.CharField(max_length=256, blank=False)
Uva_Id = models.CharField(max_length=256, blank=False)
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm
class UserFormView(View):
user_form_class = UserForm
profile_form_class= ProfileForm
#display a blank form
def get(self , request):
user_form = self.user_form_class(None)
profile_form = self.profile_form_class(None)
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
#process form data
def post(self, request):
user_form = self.user_form_class(request.POST)
profile_form = self.profile_form_class(request.POST)
user= user_form.save(commit= False)
if user_form.is_valid() and profile_form.is_valid():
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user_form.save()
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
python django
add a comment |
I used extend user model based on this tutorial.When I submit the registration form this error occurs.This errors shows for both time I use is_active() and is_active in my code.
How to resolve this error?
Error
registration form
new error
Here is my code:
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=256, blank=False)
codeforces_id = models.CharField(max_length=256, blank=False)
Uva_Id = models.CharField(max_length=256, blank=False)
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm
class UserFormView(View):
user_form_class = UserForm
profile_form_class= ProfileForm
#display a blank form
def get(self , request):
user_form = self.user_form_class(None)
profile_form = self.profile_form_class(None)
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
#process form data
def post(self, request):
user_form = self.user_form_class(request.POST)
profile_form = self.profile_form_class(request.POST)
user= user_form.save(commit= False)
if user_form.is_valid() and profile_form.is_valid():
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user_form.save()
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
python django
I used extend user model based on this tutorial.When I submit the registration form this error occurs.This errors shows for both time I use is_active() and is_active in my code.
How to resolve this error?
Error
registration form
new error
Here is my code:
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=256, blank=False)
codeforces_id = models.CharField(max_length=256, blank=False)
Uva_Id = models.CharField(max_length=256, blank=False)
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm
class UserFormView(View):
user_form_class = UserForm
profile_form_class= ProfileForm
#display a blank form
def get(self , request):
user_form = self.user_form_class(None)
profile_form = self.profile_form_class(None)
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
#process form data
def post(self, request):
user_form = self.user_form_class(request.POST)
profile_form = self.profile_form_class(request.POST)
user= user_form.save(commit= False)
if user_form.is_valid() and profile_form.is_valid():
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user_form.save()
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
python django
python django
edited Nov 21 '18 at 8:50
Ahsan Shuvo
asked Nov 21 '18 at 8:04


Ahsan ShuvoAhsan Shuvo
126
126
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You saved the form before validate.
Add user = user_form.save(commit= False)
into your if statement
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save() # You can update user object like this
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
The second error caused because when call user.save()
it automatically executes the signal defined in your model. But you commented @receiver(post_save, sender=User)
. It is crucial and uncomment it.
Thanks. but now it is showing another error. IntegrityError "NOT NULL constraint failed: website_profile.user_id". Did I miss something ?
– Ahsan Shuvo
Nov 21 '18 at 8:35
Can you post error trace
– a_k_v
Nov 21 '18 at 8:39
Please check "new error" in the post section.
– Ahsan Shuvo
Nov 21 '18 at 8:42
Did you do everything specified in the tutorial? Can you post your model
– a_k_v
Nov 21 '18 at 8:45
Please, check it again.
– Ahsan Shuvo
Nov 21 '18 at 8:50
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407601%2fdjango-valueerror-the-user-couldnt-be-created-because-the-data-didnt-validat%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You saved the form before validate.
Add user = user_form.save(commit= False)
into your if statement
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save() # You can update user object like this
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
The second error caused because when call user.save()
it automatically executes the signal defined in your model. But you commented @receiver(post_save, sender=User)
. It is crucial and uncomment it.
Thanks. but now it is showing another error. IntegrityError "NOT NULL constraint failed: website_profile.user_id". Did I miss something ?
– Ahsan Shuvo
Nov 21 '18 at 8:35
Can you post error trace
– a_k_v
Nov 21 '18 at 8:39
Please check "new error" in the post section.
– Ahsan Shuvo
Nov 21 '18 at 8:42
Did you do everything specified in the tutorial? Can you post your model
– a_k_v
Nov 21 '18 at 8:45
Please, check it again.
– Ahsan Shuvo
Nov 21 '18 at 8:50
|
show 7 more comments
You saved the form before validate.
Add user = user_form.save(commit= False)
into your if statement
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save() # You can update user object like this
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
The second error caused because when call user.save()
it automatically executes the signal defined in your model. But you commented @receiver(post_save, sender=User)
. It is crucial and uncomment it.
Thanks. but now it is showing another error. IntegrityError "NOT NULL constraint failed: website_profile.user_id". Did I miss something ?
– Ahsan Shuvo
Nov 21 '18 at 8:35
Can you post error trace
– a_k_v
Nov 21 '18 at 8:39
Please check "new error" in the post section.
– Ahsan Shuvo
Nov 21 '18 at 8:42
Did you do everything specified in the tutorial? Can you post your model
– a_k_v
Nov 21 '18 at 8:45
Please, check it again.
– Ahsan Shuvo
Nov 21 '18 at 8:50
|
show 7 more comments
You saved the form before validate.
Add user = user_form.save(commit= False)
into your if statement
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save() # You can update user object like this
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
The second error caused because when call user.save()
it automatically executes the signal defined in your model. But you commented @receiver(post_save, sender=User)
. It is crucial and uncomment it.
You saved the form before validate.
Add user = user_form.save(commit= False)
into your if statement
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save() # You can update user object like this
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active():
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
The second error caused because when call user.save()
it automatically executes the signal defined in your model. But you commented @receiver(post_save, sender=User)
. It is crucial and uncomment it.
edited Nov 21 '18 at 9:10
answered Nov 21 '18 at 8:11
a_k_va_k_v
794112
794112
Thanks. but now it is showing another error. IntegrityError "NOT NULL constraint failed: website_profile.user_id". Did I miss something ?
– Ahsan Shuvo
Nov 21 '18 at 8:35
Can you post error trace
– a_k_v
Nov 21 '18 at 8:39
Please check "new error" in the post section.
– Ahsan Shuvo
Nov 21 '18 at 8:42
Did you do everything specified in the tutorial? Can you post your model
– a_k_v
Nov 21 '18 at 8:45
Please, check it again.
– Ahsan Shuvo
Nov 21 '18 at 8:50
|
show 7 more comments
Thanks. but now it is showing another error. IntegrityError "NOT NULL constraint failed: website_profile.user_id". Did I miss something ?
– Ahsan Shuvo
Nov 21 '18 at 8:35
Can you post error trace
– a_k_v
Nov 21 '18 at 8:39
Please check "new error" in the post section.
– Ahsan Shuvo
Nov 21 '18 at 8:42
Did you do everything specified in the tutorial? Can you post your model
– a_k_v
Nov 21 '18 at 8:45
Please, check it again.
– Ahsan Shuvo
Nov 21 '18 at 8:50
Thanks. but now it is showing another error. IntegrityError "NOT NULL constraint failed: website_profile.user_id". Did I miss something ?
– Ahsan Shuvo
Nov 21 '18 at 8:35
Thanks. but now it is showing another error. IntegrityError "NOT NULL constraint failed: website_profile.user_id". Did I miss something ?
– Ahsan Shuvo
Nov 21 '18 at 8:35
Can you post error trace
– a_k_v
Nov 21 '18 at 8:39
Can you post error trace
– a_k_v
Nov 21 '18 at 8:39
Please check "new error" in the post section.
– Ahsan Shuvo
Nov 21 '18 at 8:42
Please check "new error" in the post section.
– Ahsan Shuvo
Nov 21 '18 at 8:42
Did you do everything specified in the tutorial? Can you post your model
– a_k_v
Nov 21 '18 at 8:45
Did you do everything specified in the tutorial? Can you post your model
– a_k_v
Nov 21 '18 at 8:45
Please, check it again.
– Ahsan Shuvo
Nov 21 '18 at 8:50
Please, check it again.
– Ahsan Shuvo
Nov 21 '18 at 8:50
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407601%2fdjango-valueerror-the-user-couldnt-be-created-because-the-data-didnt-validat%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown