How `default` and `default_if_none` filters set a default display value for possibly empty variables?

In website content management, we often encounter situations where variable values may be empty.These empty values, if displayed directly on the frontend page, may cause the content to be displayed incomplete, page layout to be disordered, and even bring a bad experience to the user.AnQiCMS (AnQiCMS) is a powerful content management system that provides us with a flexible way to handle such issues with the Django template engine syntax.defaultanddefault_if_noneThese two filters are the tools to display default values when variables are empty.


defaultFilter: Provide a universal alternative for 'empty' values.

Imagine you are building a detail page for an article, where there is a field to display the author of the article. If an article does not specify an author, or if the author field is exactly an empty string, you certainly do not want the page to display a blank space or an ugly(null). At this point,defaultThe filter comes into play.

defaultThe filter is like buying a 'universal insurance' for the variable.When it finds that the value of the variable is "empty", it will automatically display the default value you set.

  • empty string("")
  • Number 0(0)
  • Booleanfalse(false)
  • and represents no valuenil(Empty value)

Its usage is very intuitive, just add it to the end of a variable|default:"默认值"Just do it.

Example:

Assuming we have a variablearticle.Authorto display the author of the article.

  • If we want toarticle.AuthorDisplay "Anonymous Author" when empty:

    {{ article.Author|default:"匿名作者" }}
    

    Ifarticle.AuthorIf it is "Zhang San", it will display "Zhang San"; if it is an empty string""it will display "Anonymous Author"; if it isnilIt will also display “Anonymous Author”.

  • For example, displaying a user's points.user.ScoreIf the user has no points or the points are 0, we hope to display “No points”:

    {{ user.Score|default:"暂无积分" }}
    

    Ifuser.ScoreIs100to display100If it is0Show "No points available".

BydefaultFilter, we can easily keep the page content readable and user-friendly when variables are missing or empty.


default_if_noneFilter: A precise substitute for 'true' null values

However, in some scenarios, we may need more precise control. For example, a URL field for an imagearticle.ImageUrlIf it is explicitly set tonil(indicating that the image is not present), we want to display a placeholder. But if it is an empty string""(It may mean that the image address is empty, but the field itself exists), we may want to keep it as is, or use a different logic. At this point,default_if_nonethe filter seems more 'accurate'.

default_if_noneThe filter focuses on determining whether a variable is truly an empty value (nilwhich is also called in many programming languagesnull). It will not consider an empty string""numbers0or a boolean valuefalseConsider a 'null' value to be required. It will apply the default value you set onlynilwhen the value of the variable is

Example:

  • If there is a variableoptionalImagemay benil(No image), we want to set a placeholder for it:
    
    <img src="{{ optionalImage|default_if_none:"/static/images/placeholder.jpg" }}" alt="图片" />
    
    IfoptionalImagehas a value ofnil, the image'ssrcwill be/static/images/placeholder.jpg. But ifoptionalImagethe value is an empty string""thensrcthe property will still be""This isdefaultThe behavior of the filter is different.

How to choose:defaultOrdefault_if_none?

The choice of which filter to use depends on your definition of 'empty' and your content display needs:

  • UsedefaultFilterWhen you want all 'appearing' empty values (includingnilempty strings, the number 0, and boolean valuesfalse) to be replaced with a general default value,defaultIt is your first choice. It is more suitable for most general scenarios and can effectively prevent blank or unfriendly information from appearing on the page.

  • Usedefault_if_noneFilterIf you are dealing with an empty string, a number 0, or a boolean valuefalseThere is a specific display requirement, only wish to be applied to variablesExplicitly indicates "no value"(nil)Then the default value should be applied,default_if_noneProvide finer control. This is especially useful when you need to distinguish between the two states of 'not set' and 'set to empty'.

In Anqi CMS template development, these two seemingly similar filters actually provide control at different granularities.Understanding the subtle differences can help us write more robust and business logic-compliant front-end templates, ensuring consistent display of website content, elegant and reliable.


Frequently Asked Questions (FAQ)

Q1:defaultanddefault_if_noneWhat is the main difference between filters?A1: The main difference lies in their definition of 'empty'.defaultThe filter will also treatnilAnd an empty string (""), numbers0and boolean valuesfalseare all considered as 'empty' values that need to be replaced. Anddefault_if_nonethe filter will only occur when the variable's value isnil(No value is represented) when replacing, it will retain empty strings and numbers0and boolean valuesfalseThe original display.

Q2: Can I use these filters on non-string types (such as numbers, boolean values)?A2: Yes, of course. These filters are not limited to string variables. For example, if a numeric variable{{ price }}may benilor0, you can use{{ price|default:9.99 }}Make sure to always display a price. Similarly, boolean values{{ is_active }}If it isnilorfalseYou can also use{{ is_active|default:"否" }}To display a default text.

Q3: If I want the default value itself to be a variable, how should I write it?A3: If your default value is a variable, you can use it directly in the filter parameters. For example, suppose you have a variabledefault_author_nameStored the name of the default author, you can use it like this:

{{ article.Author|default:default_author_name }}

Or fordefault_if_none:

{{ optional_value|default_if_none:fallback_value_variable }}