Home > Software design >  Problems with validators in django models
Problems with validators in django models

Time:04-26

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. enter image description here

CodePudding user response:

You need to render the errors of the fields, so:

{{ profE_form.non_field_errors }}
{% for fieldProfile in profE_form %}
<p>
    {{ fieldProfile.errors }}
    {{ fieldProfile.label_tag }}
    {{ fieldProfile }}
</p>
{% endfor %}

You should also render the profE_form.non_field_errors. For more information, see the Rendering fields manually section of the documentation.

You should not implement the .clean_…() methods, and definitely not where you call is_valid() since Django calls these .clean_…() to check if the form is valid.

You can specify the min and/or max by specifying the widget:

from django.forms.widgets import NumberInput

class ProfileUpForm(forms.ModelForm):
    class Meta:
        model= UserProfile
        fields = ('age', 'height', 'gender', )
        widgets = {
            'age': NumberInput(attrs=dict(min=1, max=100)),
            'height': NumberInput(attrs=dict(min=0))
        }

You can simplify the view by passing the instance to the form with:

from django.contrib.auth.decorators import login_required

@login_required
def edit_view(request):
    if request.method == 'POST':
        form = ProfileUpForm(request.POST, request.FILES, instance=request.user.userprofile)
        if form.is_valid():
            form.save()
            return redirect('/profPage')
    else:
        form = ProfileUpForm(instance=request.user.userprofile)

    context = {'profE_form': form}
    return render(request, 'editPage.html', context)

Note: You can limit views to a view to authenticated users with the @login_required decorator [Django-doc].

  • Related