Django templates problem — {% if object|length > 4 %} raises TemplateDoesNotExist: 500.html - comparison

I have the following in my template.
{% block content %}
{% for album in albumsList %}
{% if fotosList %}
<div class="photoalbum-wrapper">
<h3>{{ album.title }}</h3>
<ul class="photoalbum">
{% for foto in fotosList %}<li>item</li>{% endfor %}
</ul>
{% if fotosList|length > 4 %}
больше <span>▼</span>
{% endif %}
</div>
{% endif %}
{% endfor %}
{% endblock %}
And it raises TemplateDoesNotExist: 500.html.
If i write simple {{ fotoList|length }} it works okay.

This is a very old question. Since then, newer versions of Django support operators in if-statement out of the box, so the following code will work just fine:
{% if fotosList|length > 4 %}
{% endif %}

Use fotosList.count instead of fotosList|length. you will get desired result.

FYI if tags with the operators ==, !=, <, >, <=, >= are supported now in the development version of Django.

{% if fotosList|length > 4 %} is not a valid tag; you can't use greater than/less than operators in the Django if tag. (You can use operators in the latest development release, but I'm assuming you're not using the latest version from Django's SVN repository.)
The reason you get the TemplateDoesNotExist error is because Django is throwing a 500 Internal Server Error (due to the invalid tag), but you haven't supplied a 500.html error template, as noted here.

Related

The Connection Admin does not exist

Does anyone know how to resolve this error please? I have checked and the admin site is registered in the URLS file. In all the admin.py files the relevant models are all registered. I have moved django.contrib.auth to the top of my applications in settings.py. I have also dropped and re-created the default database.
Please note I use database routers
ConnectionDoesNotExist at /admin/
The connection admin doesn't exist
Request Method: GET
Request URL: http://192.168.1.118:9000/admin/
Django Version: 2.1.15
Exception Type: ConnectionDoesNotExist
Exception Value:
The connection admin doesn't exist
Exception Location: C:\Users\Nf00038\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py in ensure_defaults, line 169
Python Executable: C:\Users\Nf00038\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.5
Python Path:
['G:\\Shares\\Website\\Development\\Intranet\\HealthcareProject4',
'C:\\Users\\Nf00038\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\Nf00038\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\Nf00038\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\Nf00038\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\Nf00038\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages',
'C:\\Users\\Nf00038\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32',
'C:\\Users\\Nf00038\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32\\lib',
'C:\\Users\\Nf00038\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\Pythonwin']
Server time: Sun, 10 Apr 2022 13:45:50 +0000
Error during template rendering
In template C:\Users\Nf00038\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\templates\admin\base.html, error at line 0
The connection admin doesn't exist
1 {% load i18n static %}<!DOCTYPE html>
2 {% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
3 <html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
4 <head>
5 <title>{% block title %}{% endblock %}</title>
6 <link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
7 {% block extrastyle %}{% endblock %}
8 {% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% static "admin/css/rtl.css" %}{% endblock %}">{% endif %}
9 {% block extrahead %}{% endblock %}
10 {% block responsive %}

Is it possible to share HubL macros across modules and templates?

In the HubSpot CMS, you can create a macro like so:
{% macro render_section(classes, background_style) %}
<div class="mosaic-section {{classes}}" {{background_style}}>
{{ caller() }}
</div>
{% endmacro %}
Is it possible to share this macro across different modules and templates? Or do you have to repeat the macro everywhere you need to use it?
Yes, you can share macros across modules but only the modules that are in the same scope as an imported HTML partial/snippet that contains the macros you've created.
According to the HubL docs for using the {% import %} tag (found here), macros can be imported from user created HTML partials/snippets. For example, if you were to create the following HTML partial called macros.html:
{% macro render_section(classes, background_style='', data='') %}
<div class="mosaic-section {{classes}}" {{background_style}} {{data}}>
{{ caller() }}
</div>
{% endmacro %}
You would then import macros.html into a template, for example, called homepage.html with the following HubL/HTML code:
<!doctype html>
<head>
{# standard HubL header code goes here %}
</head>
<body class="site-page two-column {{ builtin_body_classes }}" style="">
{% import 'path/to/macros.html as module_macros %}
<!--- more HubL/HTML code .... -->
{{ standard_footer_includes }}
</body>
</html>
And as result, all modules added to the homepage.html coded template are now in the same scope as the imported macros and as such said modules can now utilize the macros.
If you'd like to import a single macro from a HTML partial that contains multiple macros you can use the {% from %} tag (found here) and perform the following:
{% from 'path/to/macros.html' import render_section %}
Now the macro render_section() is available to all proceeding modules in the coded template.
NOTE:
Unfortunately, I have yet to find a way to use/import macros "globally" into a drag-and-drop template -- at least without having to use an embed HubL module which can add weird spacing issues in the generated HTML markup that you may need to resolve using CSS.
Hope this answer was sufficient and helps resolve the problem you've posed.

zendesk - semantic template array accessor

Is there a way for me to access a particular element of an array using Zendesk semantic templating. https://connect.zendesk.com/hc/en-us/articles/115010914668-Using-the-email-editor#topic_h2z_hn5_r1b
Normally you would go through each element of the array
{% for image in 'images' | EventProperty %}
<img src="{{ image.src }}">
{% endfor %}
But for my current use case I only need the first element. I wanna do something similar to the following:
<img src="{{ images[0].src }}">
The syntax looks like Liquid Markup, and I know they use Liquid in other areas of the product. Have you tried the Liquid Filter 'first'?
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array.first }}

Ruby dynamic binding variable as object

Consider the following loop
{% for product in collections.settings[collection_setting].products %}
{{ settings[collection_setting] }} - {{ product.title }}
{% endfor %}
Here settings[collection_setting] is equal to green but this does not work. If I have this static it works example
{% for product in collections.green.products %}
{{ settings[collection_setting] }} - {{ product.title }}
{% endfor %}
Can anyone please explain why this happens and how to solve this.
Answer
Sorry for wasting your time. This helps me to work.
Working
{% for product in collections[settings[collection_setting]].products %}
{% endfor %}
collection_setting seems to be a ruby variable. Hence to have the liquid template engine evaluate this, you'll need to have it in double braces ({{collection_setting}}).
Where you have this:
{% for product in collections.settings[collection_setting].products %}
...are you sure you don't mean this?
{% for product in collections[collection_setting].products %}
Your non-working example has a .settings where your working example does not.

include file without swig parsing it as a template

I would like to include a file in a swig template without swig parsing it as a swig template. Something like raw tag for files.
Using swig-highlight I would like to get this working.
{% highlight 'html' %}
{% import 'some-template-file.html' %}
{% endhighlight %}
Problem is 'some-template-file.html' is a Jekyll template and throws errors in swig.
You can try using the raw tag. This ignores any inside swig syntax and should, presumably, ignore any other syntax as well.
// inside (e.g.) index.html
{% import 'some-template-file.html' %}`
// inside some-template-file.html
{% raw %} {{ testVar }} {% endraw %}
// results in "{{ testVar }}"

Resources