Skip to main content
Solved

Adding Catalog Category to a condition.


Forum|alt.badge.img+3

Please find the code I wrote in Django below.
My problem is that the template doesn't render whenever I attempt to add this condition to my Abandoned Checkout Block. I just want to know if someone in the community knows enough to verify that the code syntax is correct.

{% catalog item.ProductID unpublished="cancel" %}

{% elif "Baume & Mercier" in item.ProductCategories and has_category catalog_item "watch" as in_category and has_category catalog_item "Special Savings" as in_category or has_category catalog_item "Popular" as in_category %}

{% endcatalog %}


Thanks!!
 

Best answer by Amos Peace

Hi ​@4nkown,

 

Your Django template syntax has multiple issues that could prevent it from rendering correctly. Here are the key problems and suggestions for fixing them:

Problems in Your Code:

  1. Incorrect Use of {% elif %} Without an Opening {% if %} Block

    • {% elif %} cannot be the first statement; it must follow {% if %}.
  2. Invalid {% catalog %} Block Usage

    • {% catalog %} and {% endcatalog %} are not standard Django template tags.
    • If they are custom template tags, ensure they are correctly defined in your template library.
  3. Incorrect has_category Syntax

    • has_category catalog_item "watch" as in_category should be wrapped inside an {% if %} statement.
  4. Logical Operators Misplacement

    • The or operator should be explicitly used between conditions, not within a variable assignment.
    • The multiple as in_category assignments are likely incorrect.

 Here's a simplified and corrected version:

{% catalog item.ProductID unpublished="cancel" %} {% if "Baume & Mercier" in item.ProductCategories %} {% with has_category catalog_item "watch" as watch %} {% with has_category catalog_item "Special Savings" as special %} {% with has_category catalog_item "Popular" as popular %} {% if watch or special or popular %} <!-- Content here --> {% endif %} {% endwith %} {% endwith %} {% endwith %} {% endif %} {% endcatalog %}

 

Fixes & Improvements:

Replaced {% elif %} with {% if %} for proper conditional logic.
Used {% with %} to store has_category results cleanly.
Ensured correct or logic inside {% if %}.

Next Steps:

  • Verify that {% catalog %} is a valid custom tag.
  • Ensure has_category correctly returns True or False.
  • Use {% debug %} to check variable values in the template.

 

Hope this helps.

 

Best Regards.

View original
Did this topic or the replies in the thread help you find an answer to your question?

4 replies

Byrne C
Community Manager
Forum|alt.badge.img+10
  • Community Manager
  • 64 replies
  • January 23, 2025

Hi ​@4nkown 

Did you manage to figure this out yet?

One thing I noticed is that your if/else statement starts with elif, rather than if. If/else statements need to begin with {% if and end with an {% endif %} tag. Without those, the statement won’t work, and the syntax will be invalid. I’d recommend making these changes, and seeing if that solves the issue!

Let me know if you have any additional questions

-Byrne


Forum|alt.badge.img+3
  • Author
  • Contributor II
  • 3 replies
  • January 23, 2025

well, actually it is just a piece of my code that’s why it starts with elif instead of if statement, ..and says i managed to make changes here.


chloe.strange
Community Manager
Forum|alt.badge.img+41
  • Community Manager
  • 425 replies
  • February 3, 2025

Hi ​@4nkown

I wanted to follow up to clarify if you are still looking for some guidance?

~Chloe


Amos Peace
Problem Solver III
Forum|alt.badge.img+5
  • Problem Solver III
  • 47 replies
  • Answer
  • February 3, 2025

Hi ​@4nkown,

 

Your Django template syntax has multiple issues that could prevent it from rendering correctly. Here are the key problems and suggestions for fixing them:

Problems in Your Code:

  1. Incorrect Use of {% elif %} Without an Opening {% if %} Block

    • {% elif %} cannot be the first statement; it must follow {% if %}.
  2. Invalid {% catalog %} Block Usage

    • {% catalog %} and {% endcatalog %} are not standard Django template tags.
    • If they are custom template tags, ensure they are correctly defined in your template library.
  3. Incorrect has_category Syntax

    • has_category catalog_item "watch" as in_category should be wrapped inside an {% if %} statement.
  4. Logical Operators Misplacement

    • The or operator should be explicitly used between conditions, not within a variable assignment.
    • The multiple as in_category assignments are likely incorrect.

 Here's a simplified and corrected version:

{% catalog item.ProductID unpublished="cancel" %} {% if "Baume & Mercier" in item.ProductCategories %} {% with has_category catalog_item "watch" as watch %} {% with has_category catalog_item "Special Savings" as special %} {% with has_category catalog_item "Popular" as popular %} {% if watch or special or popular %} <!-- Content here --> {% endif %} {% endwith %} {% endwith %} {% endwith %} {% endif %} {% endcatalog %}

 

Fixes & Improvements:

Replaced {% elif %} with {% if %} for proper conditional logic.
Used {% with %} to store has_category results cleanly.
Ensured correct or logic inside {% if %}.

Next Steps:

  • Verify that {% catalog %} is a valid custom tag.
  • Ensure has_category correctly returns True or False.
  • Use {% debug %} to check variable values in the template.

 

Hope this helps.

 

Best Regards.