我正在尝试使用Django实现博客应用。我创建了带有个人资料图片上传的注册表单。但是在“ user_id”列中其返回的完整性错误null值违反了非null约束 详细信息:失败行包含(7,pics / P_Wk6m1b3.png,空)。
#models.py
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg',upload_to='pics')
#views.py
def register(request):
if request.method == "POST":
form = Register(request.POST,request.FILES)
if form.is_valid():
profile = Profile()
email = form.cleaned_data['Email']
User_name=form.cleaned_data['Username']
Password=form.cleaned_data['Password']
Confirm_Password=form.cleaned_data['Confirm_Password']
firstname=form.cleaned_data['Firstname']
user=User.objects.create_user(username=User_name,
password=Password,email=email,first_name=firstname)
user.save();
insert = Profile(image = request.FILES['picture'],
user_id=request.user.id)
insert.save()
return redirect('/')
else:
form = Register()
return render(request,'register.html',{'form': form})
#forms.py
class Register(forms.Form):
Email = forms.EmailField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}))
Username = forms.CharField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}))
Password = forms.CharField(widget=forms.PasswordInput(attrs=
({"class":"inputvalues"})))
Firstname = forms.CharField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}),max_length=30)
Lastname = forms.CharField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}),max_length=40)
Confirm_Password = forms.CharField
(widget=forms.PasswordInput(attrs=({"class":"inputvalues"})))
Image = forms.ImageField()
def clean_Email(self):
if validate_email(self.cleaned_data['Email']):
raise forms.ValidationError("Email is not in correct format!")
elif User.objects.filter(email = self.cleaned_data['Email'])
.exists():
raise forms.ValidationError("Email aready exist!")
return self.cleaned_data['Email']
def clean_Username(self):
if User.objects.filter(username =
self.cleaned_data['Username']).exists():
raise forms.ValidationError("Username already exist!")
return self.cleaned_data['Username']
def clean_Confirm_Password(self):
pas=self.cleaned_data['Password']
cpas = self.cleaned_data['Confirm_Password']
if pas != cpas:
raise forms.ValidationError("Password and Confirm Password are not
matching!")
else:
if len(pas) < 8:
raise forms.ValidationError("Password should have atleast 8
character")
if pas.isdigit():
raise forms.ValidationError("Password should not all numeric")
<!-------register.html>
{% extends 'layout.html' %}
{% block content %}
<div class="box">
<h2>
<center>Register</center>
</h2><br>
<form action='register' method='POST'>
{% csrf_token %}
<div>
<label class='labe'>Image:</label>
{{ form.Image }}
</div>
<div>
<label class='labe'>First name:</label>
{{ form.Firstname }}
</div>
<div>
<label class='labe'>Last name:</label>
{{ form.Lastname }}
</div>
<div>
<label class='labe'>Email:</label>
{{ form.Email }}
{{ form.Email.errors }}
</div>
<div>
<label class='labe'>Username:</label>
{{ form.Username }}
{{ form.Username.errors }}
</div>
<div>
<label class='labe'>Password:</label>
{{ form.Password }}
</div>
<div>
<label class='labe'>Confirm Password:</label>
{{ form.Confirm_Password }}
{{ form.Confirm_Password.errors }}
</div>
<input type="Submit" id="lg"/><br>
<center><a href="login" >Already have an account.Login here.
</a></center>
</form>
</div>
<div>
{% for message in messages%}
<h1>{{message}}</h1>
{% endfor %}
</div>
{% endblock %}
返回完整性错误,我想知道那是什么7.每次输入数据并每次单击提交时,它都会一一增加
答案 0 :(得分:1)
request.user
是当前登录的用户。但是没有登录用户,因为这是注册表格。您需要使用刚创建的用户。
user=User.objects.create_user(username=User_name,
password=Password,email=email,first_name=firstname)
insert = Profile(image = request.FILES['picture'],
user=user)
insert.save()
答案 1 :(得分:0)
您正在register
中创建用户记录,但是使用request.user.id,我希望它是None,因为用户尚未登录。
相反,直接将用户传递给:
insert = Profile(image = request.FILES['picture'], user=user)