Django Error: Ensure this value has at most 10 characters (it has 321)
Django Error: Ensure this value has at most 10 characters (it has 321)
我有更新用户详细信息的页面文件。 (学生用户)
我为学生创建字段:
ID = models.CharField(max_length=10, null=True)
我也在 StudentForm 中做 clean_ID,但是当我按下正确数量的字符时
我得到了这个错误:
Ensure this value has at most 10 characters (it has 321).
你可以在下面的照片中看到这一点。
我不明白如何解决这个问题。
查看文件
class DetailsStudentView(View):
def get(self,request, student_id):
student = Student.objects.get(pk=student_id)
userStudent = User.objects.get(pk=student_id)
form_student = StudentForm(request.POST or None, instance=student)
form_user = StudentUserUpdate(None,instance=userStudent)
return render(request, "details.html",{'form_student':form_student, 'form_user':form_user})
def post(self,request,student_id):
form_student = StudentForm(request.POST, request.FILES)
if form_student.is_valid():
user = User.objects.get(pk=student_id)
email = form_student.cleaned_data['email']
user.email = email
user.save()
student = Student.objects.get(pk=student_id)
student.first_name = form_student.cleaned_data['first_name']
student.last_name = form_student.cleaned_data['last_name']
student.Phone = form_student.cleaned_data['Phone']
img = request.POST.get('imag_profile')
if img != None:
student.imag_profile = img
student.ID = form_student.cleaned_data['ID']
student.save()
return redirect('HomePage:home')
userStudent = User.objects.get(pk=student_id)
form_user = StudentUserUpdate(None,instance=userStudent)
return render(request,'details.html',{'form_student':form_student, 'form_user':form_user})
表单文件
class StudentForm(forms.ModelForm):
email = forms.EmailField(widget = forms.TextInput(attrs={ 'type':"text" ,'class': "bg-light form-control" }))
class Meta():
model = Student
fields = ('first_name','last_name','Phone','imag_profile','ID')
widgets = {
'first_name': forms.TextInput(attrs={ 'type':"text" ,'class':"bg-light form-control", }),
'last_name': forms.TextInput(attrs={ 'type':"text" ,'class':"bg-light form-control", }),
'Phone': forms.TextInput(attrs={ 'type':"text" ,'class':"bg-light form-control", }),
'ID': forms.TextInput(attrs={ 'type':"text" ,'class':"bg-light form-control", }),
}
def clean_ID(self):
ID = self.cleaned_data.get('ID')
if not ID.isdecimal():
raise forms.ValidationError("ID must to contain only digits.")
elif len(ID) != 9:
raise forms.ValidationError("ID must to contain 9 digits.")
return self.cleaned_data
HTML 文件
{% block content %}
<form method="post" enctype="multipart/form-data">
<div class="wrapper bg-white mt-sm-5">
<h4 class="pb-4 border-bottom">Account settings</h4>
<div class="d-flex align-items-start py-3 border-bottom">
{% csrf_token %}
<img src="https://images.pexels.com/photos/1037995/pexels-photo-1037995.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" class="img" alt="">
<div class="pl-sm-4 pl-2" id="img-section">
<label for="imag_profile"><b>Profile Photo</b></label>
<p>Accepted file type .png. Less than 1MB</p>
{{form_student.imag_profile}}
</div>
</div>
<div class="py-2">
<div class="row py-2">
<div class="col-md-6">
<label for="first_name">שם פרטי</label>
{{form_student.first_name}}
</div>
<div class="col-md-6 pt-md-0 pt-3">
<label for="last_name">שם משפחה</label>
{{form_student.last_name}}
</div>
</div>
<div class="row py-2">
<div class="col-md-6">
<label for="email">מייל המכללה</label>
{{form_user.email}}
</div>
<div class="col-md-6 pt-md-0 pt-3">
<label for="Phone">מספר פלאפון</label>
{{form_student.Phone}}
</div>
</div>
<div class="row py-2">
<div class="col-md-6">
<label for="ID">תעודת זהות</label>
{{form_student.ID}}
{% if form_student.errors %}
<div class="alert alert-danger" role="alert">
{{form_student.ID.errors}}
</div>
{% endif %}
</div>
</div>
</form>
<div class="py-3 pb-4 border-bottom">
<button class="btn btn-primary mr-3">Save Changes</button>
<a href="{% url 'HomePage:home'%}" class="btn border button">Cancel</a>
<a href="{% url 'Details:ch-pass' request.user.id %}" class="changePass">שינוי סיסמא</a>
</div>
<div class="d-sm-flex align-items-center pt-3" id="deactivate">
<div>
<b>Deactivate your account</b>
<p>Details about your company account and password</p>
</div>
<div class="ml-auto">
<a href="{% url 'Details:delete-user' request.user.id %}" class="btn danger">Delete User</a>
</div>
</div>
</div>
</div>
{% endblock %}
我也在删除数据库并制作
迁移
和
迁移
但是钢铁我遇到了这个问题。
Always return a value to use as the new cleaned data, even if this
method didn't change it. The return value of this method replaces the existing value in cleaned_data, so it must be the field’s value from cleaned_data (even if this method didn’t change it) or a new cleaned value.
因此对于您的情况,您可能希望将 clean_id
修改为以下 return 值,而不是全部 cleaned_data
:
def clean_ID(self):
ID = self.cleaned_data.get('ID')
if not ID.isdecimal():
raise forms.ValidationError("ID must to contain only digits.")
elif len(ID) != 9:
raise forms.ValidationError("ID must to contain 9 digits.")
# return self.cleaned_data # <--- DON'T DO THIS
return ID
我有更新用户详细信息的页面文件。 (学生用户) 我为学生创建字段:
ID = models.CharField(max_length=10, null=True)
我也在 StudentForm 中做 clean_ID,但是当我按下正确数量的字符时 我得到了这个错误:
Ensure this value has at most 10 characters (it has 321).
你可以在下面的照片中看到这一点。 我不明白如何解决这个问题。
查看文件
class DetailsStudentView(View):
def get(self,request, student_id):
student = Student.objects.get(pk=student_id)
userStudent = User.objects.get(pk=student_id)
form_student = StudentForm(request.POST or None, instance=student)
form_user = StudentUserUpdate(None,instance=userStudent)
return render(request, "details.html",{'form_student':form_student, 'form_user':form_user})
def post(self,request,student_id):
form_student = StudentForm(request.POST, request.FILES)
if form_student.is_valid():
user = User.objects.get(pk=student_id)
email = form_student.cleaned_data['email']
user.email = email
user.save()
student = Student.objects.get(pk=student_id)
student.first_name = form_student.cleaned_data['first_name']
student.last_name = form_student.cleaned_data['last_name']
student.Phone = form_student.cleaned_data['Phone']
img = request.POST.get('imag_profile')
if img != None:
student.imag_profile = img
student.ID = form_student.cleaned_data['ID']
student.save()
return redirect('HomePage:home')
userStudent = User.objects.get(pk=student_id)
form_user = StudentUserUpdate(None,instance=userStudent)
return render(request,'details.html',{'form_student':form_student, 'form_user':form_user})
表单文件
class StudentForm(forms.ModelForm):
email = forms.EmailField(widget = forms.TextInput(attrs={ 'type':"text" ,'class': "bg-light form-control" }))
class Meta():
model = Student
fields = ('first_name','last_name','Phone','imag_profile','ID')
widgets = {
'first_name': forms.TextInput(attrs={ 'type':"text" ,'class':"bg-light form-control", }),
'last_name': forms.TextInput(attrs={ 'type':"text" ,'class':"bg-light form-control", }),
'Phone': forms.TextInput(attrs={ 'type':"text" ,'class':"bg-light form-control", }),
'ID': forms.TextInput(attrs={ 'type':"text" ,'class':"bg-light form-control", }),
}
def clean_ID(self):
ID = self.cleaned_data.get('ID')
if not ID.isdecimal():
raise forms.ValidationError("ID must to contain only digits.")
elif len(ID) != 9:
raise forms.ValidationError("ID must to contain 9 digits.")
return self.cleaned_data
HTML 文件
{% block content %}
<form method="post" enctype="multipart/form-data">
<div class="wrapper bg-white mt-sm-5">
<h4 class="pb-4 border-bottom">Account settings</h4>
<div class="d-flex align-items-start py-3 border-bottom">
{% csrf_token %}
<img src="https://images.pexels.com/photos/1037995/pexels-photo-1037995.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" class="img" alt="">
<div class="pl-sm-4 pl-2" id="img-section">
<label for="imag_profile"><b>Profile Photo</b></label>
<p>Accepted file type .png. Less than 1MB</p>
{{form_student.imag_profile}}
</div>
</div>
<div class="py-2">
<div class="row py-2">
<div class="col-md-6">
<label for="first_name">שם פרטי</label>
{{form_student.first_name}}
</div>
<div class="col-md-6 pt-md-0 pt-3">
<label for="last_name">שם משפחה</label>
{{form_student.last_name}}
</div>
</div>
<div class="row py-2">
<div class="col-md-6">
<label for="email">מייל המכללה</label>
{{form_user.email}}
</div>
<div class="col-md-6 pt-md-0 pt-3">
<label for="Phone">מספר פלאפון</label>
{{form_student.Phone}}
</div>
</div>
<div class="row py-2">
<div class="col-md-6">
<label for="ID">תעודת זהות</label>
{{form_student.ID}}
{% if form_student.errors %}
<div class="alert alert-danger" role="alert">
{{form_student.ID.errors}}
</div>
{% endif %}
</div>
</div>
</form>
<div class="py-3 pb-4 border-bottom">
<button class="btn btn-primary mr-3">Save Changes</button>
<a href="{% url 'HomePage:home'%}" class="btn border button">Cancel</a>
<a href="{% url 'Details:ch-pass' request.user.id %}" class="changePass">שינוי סיסמא</a>
</div>
<div class="d-sm-flex align-items-center pt-3" id="deactivate">
<div>
<b>Deactivate your account</b>
<p>Details about your company account and password</p>
</div>
<div class="ml-auto">
<a href="{% url 'Details:delete-user' request.user.id %}" class="btn danger">Delete User</a>
</div>
</div>
</div>
</div>
{% endblock %}
我也在删除数据库并制作 迁移 和 迁移
但是钢铁我遇到了这个问题。
Always return a value to use as the new cleaned data, even if this method didn't change it. The return value of this method replaces the existing value in cleaned_data, so it must be the field’s value from cleaned_data (even if this method didn’t change it) or a new cleaned value.
因此对于您的情况,您可能希望将 clean_id
修改为以下 return 值,而不是全部 cleaned_data
:
def clean_ID(self):
ID = self.cleaned_data.get('ID')
if not ID.isdecimal():
raise forms.ValidationError("ID must to contain only digits.")
elif len(ID) != 9:
raise forms.ValidationError("ID must to contain 9 digits.")
# return self.cleaned_data # <--- DON'T DO THIS
return ID