How to add this if statement to a template with Django and improve the views.py

Viewed 73

My knowledge of html and Django is very limited but I have this Django project with multiple (12) HTML pages that display information in a table similar to the code below. The table is conditionally displayed if there are any fixes left to make. If any of the data in the fixes was already tested it should be highlighted in red to avoid editing. I recently added the <tr {% if p.tested in green_tested %} bgcolor="red" {% endif %}> bit to do the highlighting but it felt really dumb to have to add it manually to all 12 pages. Is there a way that I can add it as part of an external template?

{% extends "admin/base_site.html" %}
{% load static %}

{% block content %}

<p><b>Description:</b> {{ docstring }}</p>
{% if count|length %}
    <p><b>Action:</b> Investigate why and fix.</p>
    <p style="color:red">If any table rows are highlighted red, indicating a tested profile, flag immediately to health report owner/Automation before editing.</p>
    <table>
      <tr bgcolor="#ccc">
        <th>Profile</th>
        <th>Name</th>
        <th>Type</th>
        <th>Tested</th>
        <th>e.g. Device</th>
      </tr>
    {% for p in count %}
      <tr {% if p.tested in green_tested %} bgcolor="red" {% endif %}>
        <td><a href={{ p.profile_url }}>{{ p.url }}</a></td>
        <td>{{ p.name }}</td>
        <td>{{ p.type }}</td>
        <td>{{ p.tested }}</td>
        <td><a href={{ p.device_url }}>{{ p.dev_id }}</a></td>
    {% endfor %}
      </tr>
    </table>
{% else %}
No fixes left to make!
{% endif %}

{% endblock %}

My function in the views.py looks something like this:

def test_function(request):
    count = test_function_count()
    context = {'count': count,
                'green_tested': list(map(lambda x: x[0], models.GREEN_TESTED)),
                'docstring': test_function_count.__doc__}
    return render(request, 'health_report/test-function-page.html', context)

It would also be nice to not have to add the 'green_tested' to all my functions in views.

2 Answers

For the problem with green_tested in every view, I suggest using a context processor. First You have to create a new file context_processors.py, in the same location where your view.py is. Sth like that:

def function_name(request):
    data = # here put logic you want
    return {
        'context_data': data
    }

Then You need to register your context processor in settings.py file in TEMPLATES. The pattern is: YourAppName.FileName.FunctionName Example here (I don't replace YourAppName because I don't know it):

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['YourAppName/templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',

                # Here is new line
                'YourAppName.context_processor.function_name',
            ],
        },
    },
]

Then You can in any template use {{ context_data }}, name of the key in the dictionary returned by the function.

For the primary problem, I have only a suggestion. You could create some kind of parent template, like your "base_site.html". Something between base_site.html and current template. But the line You want to place is somewhere in the middle so I'm not sure if that is gonna work for You or how these templates are similar to each other. It could make more problem than putting that line 12 times.

{% extends "admin/base_site.html" %}
{% load static %}

{% block content1 %}
{% endblock %}

 Here the code you want in each template.

{% block content2 %}
{% endblock %}
{% extends "admin/base_site.html" %}
{% load static %}

{% block content %}

<p><b>Description:</b> {{ docstring }}</p>
{% if count|length !=0 %}
    <p><b>Action:</b> Investigate why and fix.</p>
    <p style="color:red">If any table rows are highlighted red, indicating a tested profile, flag immediately to health report owner/Automation before editing.</p>
    <table>
      <tr bgcolor="#ccc">
        <th>Profile</th>
        <th>Name</th>
        <th>Type</th>
        <th>Tested</th>
        <th>e.g. Device</th>
      </tr>
    {% for p in count %}
      <tr {% if p.tested in green_tested %} bgcolor="red" {% endif %}>
        <td><a href="{{ p.profile_url }}">"{{ p.url }}"</a></td>
        <td>{{ p.name }}</td>
        <td>{{ p.type }}</td>
        <td>{{ p.tested }}</td>
        <td><a href={{ p.device_url }}>{{ p.dev_id }}</a></td>
    {% endfor %}
      </tr>
    </table>
{% else %}
No fixes left to make!
{% endif %}

{% endblock %}
Related