Use multiple layouts in one file

Hi I insert code in a template from a TextField with this tag:

        <!--START SHIFT-->
            {{ shifts.status.template_name|safe }}
        <!--END SHIFT-->

I use the |safe tag to make sure the codes are displayed properly when I view the source code it shows this:

            <div class="grid-item card" style="background-color: {{ shifts.status.color }}">

                    <div class="flex-container inverse sb">
                        <div><b>{{ shifts.start_date|date:'D j M Y'}}</b></div>
                        <div>{{ shifts.start_time }} - {{ shifts.end_time }}</div>

                        <div>{{ shifts.function }} | {{ shifts.shift_title }}</div>
                        <div><i>{{ shifts.user.first_name}} {{ shifts.user.last_name }}, <b>{{ shifts.user.Function_short }}</b></i></div>

                    <div class="flex-container sb">
                        <div>{{ }} - {{ }}</div>
                        <div><i class="bi bi-exclamation-circle-fill"></i></div>
        <!--END SHIFT-->

But now the field tags are not rendered. Am I doing something stupid?
I want to give cards with different statussen in my planning app unique layouts. Sofar I could only change the background color with a field from the status table.

Note: When I put the code directly in de template file things are working as shown below in the picture

Advice on what would be a good approach to achieve this are very welcome.
Here a preview of my template design:

Adding a template as a field to be rendered in a template is not going to cause that template itself to be rendered.

If you need to render a template within another template, you use the include tag to include the desired template.

Thanks that works!

{% include "shift.html" %}

But can I replace “shift.html” with a field? Like this:

{% include "{{ shifts.status.template_name }}" %}

The content of this field is the same template name: shift.html

I get an error when I do this.

See the docs at Built-in template tags and filters | Django documentation | Django

I stydied the docs but can’t see a solution to include a field tag in the {% include…
Is this not possible?

Yes - when you’re supplying a variable, you don’t include it in the double-braces or in quotes. The docs show that you directly supply the variable name to use.

Tried that but it does not seem to work with field tags like this:

{% include shifts.status.template_name %}


{% include "shifts.status.template_name" %}

A field tag is not the same as a variable I suppose!?

I don’t know what you mean by “field tag” in this context.

First, syntactically, the first version is correct.

Django is going to look for a key in the context named “shifts”. Then, for the object stored as “context[shifts]”, it’s going to try and retrieve the attribute named “status”. Then, for that object, it’s going to look for an attribute named “template_name”. That means that “template_name” should resolve to a string that references a template file.

This is what I mean with field tag {{ shifts.status.template_name }}

It gets the content via the ForeignKey field in the Shift model to the Status table and returns the value in the template_name field that contains: shift.html

The error:

Ok, that’s not a “field tag”. It’s not a tag at all, it’s a variable. (Yes, Django makes the distinction between the two. See The Django template language | Django documentation | Django)

What this is telling you is that that variable (shifts.status.template_name) is unable to be resolved to a value.

Looking at the larger context and seeing that you’re using a regroup, I’d say the problem here is that at this point in the template that “shifts” is not a reference to an object, but just a data element. Or, just as likely, if “shifts” is a dict or objects, almost certainly “shifts.status” is not anything more than just a data element.

Basically, to resolve this, you’ll need to identify precisely what data you have at that location.

This code of the working regroup:

{% regroup shifts by weeknr as week_list %}
    {% for weeknr in week_list %}
       <div class="grid-container">
           <div class="weeknr"><h1>Week {{ weeknr.grouper }}</h1></div>

          {% regroup weeknr.list by date_header as day_list %}

        <div class="grid-container shifts"><!--START DAY-->
            {% for date_header in day_list %}
                 {{ date_header.grouper|safe }}
                    {% for shifts in date_header.list %}

        <!--START SHIFT-->
            {{ shifts.status.template_name }}
        <!--END SHIFT-->

                {% endfor %}
            {% endfor %}
    {% endfor %}

When I replace the {% include with just the field reference this is my output:
show value

You don’t show “shift.html” for the first entry (Wednesday 28-12) - that’s what could be throwing the error.

You’re iterating over “shifts” in date_header.list, so there is an entry - but either “status” is none or “template_name” is none.

Sorry that’s confusing. On 28-12 there is a shift with an other status. Like this:
The regroup is correct and works fine. I only made this example to see what values is returned. I was hoping I could use this value in the {% include… but that does not seem te work.

Thanks for your concern Ken very appriciated!
Now bedtime here 1 am.
show value2

You can. It does work. If it’s not working, then there’s a data issue somewhere.

Since the entire template is rendered at one time, any error is going to prevent the template from being completed.

You need to verify all the data, not just some of it.

You are right!!! I did not supplied a template name for the other statuses. Now it works!!
Thanks for your help!!
This will give me a good night sleep!