0

I have a number of apps that each have their own intro section. This intro section has quite a few lines of HTML and only a few lines are adjusted for each app (Think title and intro). I would like this intro section to live at the project level (where the navbar template lives), but I can't find a way to pass template variables from the app to the project.

All of my apps(about 15) follow this template scheme:

app_base.html extends project base.html
app_base.html has an {% include %} to pull in app_intro.html
all <app_unique_templates>.html that are called by <app>/views.py, extend the app_base.html

To reiterate, how can I pass a template variable from /views.py to project base.html template using my app template scheme? If I can't use this scheme, can I adjust it in a way that will allow me to accomplish this?

Thank you all in advance!

views.py:

def add_app_context(view_context):
    app_context = {
        'app_name': 'Material Database',
        'app_desc': 'Long String goes Here For Application Description',
        'doc_link': '#',
    }
    view_context.update(app_context)
    return view_context


class MaterialList(ListView):
    model = Material
    context_object_name = 'materials'

    def get_context_data(self):
        context = super().get_context_data()
        context['pagetitle'] = 'Material List'
        context['intro_btn_link'] = '/materials/new'
        context['intro_btn_name'] = 'Create New Material'

        return add_app_context(context)

app_base.html:

{% extends "mjd_tools/base.html" %}
{% load staticfiles %}
{% block body %}
<link rel="stylesheet" href="{% static 'mat_db/css/style.css' %}">

{% include "mat_db/app_intro.html" %}

{% block list %}{% endblock list %}

{% block form %}{% endblock form %}

<script src="{% static 'mat_db/js/scripts.js' %}"></script>
{% endblock body %}

app_intro.html(this is what I have to repeat for each app).

<div class="row">
    <div class="col-sm-8">
        <h1>{{ app_name }}</h1>
    </div>
</div>
<hr>
<div class="row">
    <div class="col-sm-8">
        <p>
            {{ app_desc }}
        </p>
    </div>
    <div class="col-auto ml-auto">
        <a class="btn btn-warning" role="button" href="{{ doc_link }}">Documentation</a>
    </div>
</div>
<hr>
<div class="row">
    <div class="col-sm text-center">
        <h4>
            <span>{{ pagetitle }}</span>
            {% if intro_btn_name %}
            <a class="btn btn-primary btn-sm pull-right" role="button"
                href="{{ intro_btn_link }}">{{ intro_btn_name }}</a>
        </h4>
        {% endif %}
    </div>
</div>
<hr>
  • 1
    show us a few `views.py` functions (may be shorted, but show how you pass context). Can you show also templates more detailed? – 404pio Sep 18 '19 at 06:44
  • ok, I added in my views and templates – Mike Dergance Sep 18 '19 at 16:43
  • It looks pretty good. But I think you lack context processor. Have a look here: https://docs.djangoproject.com/en/2.2/ref/templates/api/ and here: https://stackoverflow.com/questions/2893724/creating-my-own-context-processor-in-django – 404pio Sep 18 '19 at 19:32

0 Answers0