I want to create an edit page where the customer can edit the profile page. I have a problem with the validators and I don't know how to solve this.
model.py
class UserProfile(models.Model):
CAT_G = (
('W', 'W'),
('M', 'M'),
('do not want to mention', 'do not want to mention'),
)
age = models.IntegerField(default=1, validators=[ MaxValueValidator(100), MinValueValidator(1)])
height = models.DecimalField(max_digits=3, validators=[MinValueValidator(Decimal('0.0'))], decimal_places=2)
gender = models.CharField(max_length=27, blank=False, null= False, choices=CAT_G)
view.py
def edit_view(request):
context={}
if request.method == "POST":
form = ProfileUpForm(request.POST, instance=request.user.userprofile)
if form.is_valid():
form.save()
return redirect('/profPage')
else:
form = ProfileUpForm(
initial={
"age":request.user.userprofile.age,
"height":request.user.userprofile.height,
"gender":request.user.userprofile.gender,
}
)
context['profE_form']= form
return render(request, 'editPage.html', context)
forms.py
class ProfileUpForm(forms.ModelForm):
class Meta:
model= UserProfile
fields =('age', 'height', 'gender', )
def clean_age(self):
if self.is_valid():
age=self.cleaned_data['age']
return age
def clean_height(self):
if self.is_valid():
height=self.cleaned_data['height']
return height
def clean_gender(self):
if self.is_valid():
gender=self.cleaned_data['gender']
return gender
editPage.html
{% for fieldProfile in profE_form %}
<p>
{{fieldProfile.label_tag}}
{{fieldProfile}}
</p>
{% endfor %}
The problem is that in the html page, the user can choose a negative number, even if I put that validators in my model.
