How to properly use templates as a variable in django?

In general, I wanted to use TemplateView from Django to provide responses to users. Since my website will have many frames with content blocks as separate sections, I thought it would be very convenient to store them as class objects, each having its own variables and HTML files (templates). The main idea was to include multiple templates within a single TemplateView (in other words, many small TemplateViews within one TemplateView).

The main problem I encountered is that the context_object_name variables conflict with names in other templates. How can this be resolved? Ideally, it would be great if the variable could be created locally for a specific template.

For example, I will often refer to the rfq-details.html template, and there will be many of them, so it would be perfect if each table value could be enclosed in a variable that doesn't conflict with others.

    <div class="rfq-details">
        <table>
            <tr><td>Name </td></tr>
            <tr><td>Main Characteristics of the Product  </td></tr>
            <tr><td>Consumer Characteristics of the Product  </td></tr>
            <tr><td>Type and Volume of Packaging </td></tr>
            <tr><td>Quantity (volume) Requested  </td></tr>
            <tr><td>Terms of Delivery (INCOTERMS)    </td></tr>
            <tr><td>Terms of Payment </td></tr>
            <tr><td>Expected Price Per Unit of Goods </td></tr>
            <tr><td>Compliance with Sanitary and Phytosanitary Standards </td></tr>
            <tr><td>Transportation Conditions    </td></tr>
            <tr><td>Unloading Area   </td></tr>
            <tr><td>Place of Customs Clearance   </td></tr>
            <tr><td>The Need for Third-Party Inspection</td></tr>
        </table>  
    </div>

my developments in code:

base.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
    <title>{{ data.title }}</title>
    <link rel="stylesheet" type="text/css" href="{% static 'styles.css' %}">
    <style>
        pre {
            background-color: #f0f0f0;
            border: 1px solid #ddd;
            padding: 10px;
            font-family: monospace;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        .debug-container {
            max-width: 100%;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <div class="debug-container">
        <pre>{{ debug }}</pre>
    </div>
    {% include 'header.html' %}

    <div class='content'>
        {% for template in data.templates %}
            {% include template %}
        {% endfor %}
    </div>
</body>
</html>

models.py

class Tabs():
    context_object_name = 'tabs'
    template_name       = 'tabs.html'
    active_tab          = ''

class TabBanner():
    context_object_name = 'tab_banner'
    template_name       = 'tab-banner.html'

    name                = ''
    join_date           = ''
    button_action       = ''
    button              = ''

class Data(BaseModel):
    title:          str
    templates:      List[str] = []
    user_avatar:    Optional[str] = ''

views.py

class Profile(TemplateView):
    template_name = 'base.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        
        tabs        = models.Tabs()
        tab_banner  = models.TabBanner()
        tabs.active_tab = 'profile'
        
        templates = [
            'company-banner.html',
            tabs.template_name,
            tab_banner.template_name,
            'tab-profile.html'
        ]
        data = Data(title='Profile', templates=templates)
        
        tab_banner.button       = 'edit'
        tab_banner.name         = 'Profile2'
        tab_banner.join_date    = 'Join date 12.05.2022'

        context[tabs.context_object_name] = tabs
        context[tab_banner.context_object_name] = tab_banner
   
        return context

I tried to create a system in which it was possible to render an infinite number of templates in which the variables would not intersect with each other on the back end side, store it all in a list of class objects or something like that.

Back to Top