Home > Software design >  How to avoid fields that has null values automatically when rendering html
How to avoid fields that has null values automatically when rendering html

Time:10-29

I want to not to display the null value fields in template.So How can i achieve this in django?Is there any functions available to do this.

CodePudding user response:

In you used Django ModeForm then do this

class ProductForm(forms.ModelForm):  
    class Meta:  
        model = ProductModel  
        exclude = ('selling_price','discounted_price') # this is exclude fields not render in html template

NOTE:- pass null=True, blank=True in Model

CodePudding user response:

If what you are saying is that you do not want a field that has been set to null = True in your models.py to be rendered in your html template, you can use django inbulit form class to render the form and exclude whatever field is null

Here is an example of what i am saying.

class Userdetail(forms.ModelForm):
    class Meta:
        model = User_detail
        exclude = ("name_of_null_field",) #This field won't be rendered in your html template because it is excluded.

You can read more on Django forms here Working with forms

  • Related