i have page file that i am update the details of the user. (Student User) and i create field for student:
ID = models.CharField(max_length=10, null=True)
and i also do clean_ID in StudentForm, but when i press the correct amount of characters i got the this error:
Ensure this value has at most 10 characters (it has 321).
you can see this in the photo down here. i don't understand how to fix the problem.
VIEWS FILE
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})
Form FILE
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 FILE
{% block content %}
<form method="post" enctype="multipart/form-data">
<div >
<h4 >Account settings</h4>
<div >
{% csrf_token %}
<img src="https://images.pexels.com/photos/1037995/pexels-photo-1037995.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="">
<div 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 >
<div >
<div >
<label for="first_name">שם פרטי</label>
{{form_student.first_name}}
</div>
<div >
<label for="last_name">שם משפחה</label>
{{form_student.last_name}}
</div>
</div>
<div >
<div >
<label for="email">מייל המכללה</label>
{{form_user.email}}
</div>
<div >
<label for="Phone">מספר פלאפון</label>
{{form_student.Phone}}
</div>
</div>
<div >
<div >
<label for="ID">תעודת זהות</label>
{{form_student.ID}}
{% if form_student.errors %}
<div role="alert">
{{form_student.ID.errors}}
</div>
{% endif %}
</div>
</div>
</form>
<div >
<button >Save Changes</button>
<a href="{% url 'HomePage:home'%}" >Cancel</a>
<a href="{% url 'Details:ch-pass' request.user.id %}" >שינוי סיסמא</a>
</div>
<div id="deactivate">
<div>
<b>Deactivate your account</b>
<p>Details about your company account and password</p>
</div>
<div >
<a href="{% url 'Details:delete-user' request.user.id %}" >Delete User</a>
</div>
</div>
</div>
</div>
{% endblock %}
i am also delete the db and make makemigrations and migrate
but steel i got this problem.
CodePudding user response:
Per the documentation
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.
So for your case, you probably want to amend clean_id
to the following to return the value, not all of 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