Eric Kim
Eric Kim

Reputation: 2696

Django - how to pass context in base.html?

I have well_list.html that looks like this:

enter image description here

Clicking on one of the rows will lead to this page:

enter image description here

The above page uses two html files: base.html + contextual_main.html. Base has sidebar navigation, and the latter one extends from the base. In my views.py, I can easily pass in context data into contextual_main.html like this:

views.py

class ContextualMain_DetailView(DetailView):
    template_name = 'contextual_main.html'
    context_object_name = 'single_well_info'
    model = models.WellInfo

contextual_main.html

<button type="button" class="btn btn-default" data-container="body" data-toggle="popover">
  <a href="{% url 'contextual:bha' pk=single_well_info.api %}">BHA</a>
</button>

I need to inject context, single_well_info, into base.html too. So I tried injecting it in the same way I did with contextual_main.html, but it won't work. How can I do this?

edit---------------------------------------------------------------------

base.html:

  <body class="nav-md">
    <div class="container-fluid body">
      <div class="main_container">
        <!-- side bar -->
        <div class="col-md-3">
          <div class="left_col scroll-view">
            <!-- sidebar menu -->
            <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
              <div class="menu_section">
                <ul class="nav side-menu">
                  <li><a href="{% url 'well_list' %}"><i class="fa fa-list-ol"></i> Well List </a>
                  </li>
                  <li><a><i class="fa fa-bookmark"></i> My Wells </a>
                  </li>
                  <li><a><i class="fa fa-dashboard"></i> Dashboard </a>
                  </li>
                  <li><a><i class="fa fa-edit"></i> Contextual <span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">
                      <li><a href="{% url 'contextual:bha' pk=single_well_info.api %}">BHA</a></li>
                      <li><a href="">Integrity Test</a></li>
                      <li><a href="">Casing</a></li>
                      <li><a href="">Cementing</a></li>
                      <li><a href="">Consumables</a></li>
                      <li><a href="">Daily Ops</a></li>
                      <li><a href="">Logging Data</a></li>
                      <li><a href="">Daily Cost</a></li>
                      <li><a href="">Mud</a></li>
                      <li><a href="">Equipments</a></li>
                      <li><a href="">Pumps</a></li>
                      <li><a href="">Survey</a></li>
                      <li><a href="">Time Log</a></li>
                      <li><a href="">Vol & Losses</a></li>
                      <li><a href="">Weather</a></li>
                      <li><a href="">Well Cond</a></li>
                      <li><a href="">Personnels</a></li>
                    </ul>
                  </li>
                  <li><a><i class="fa fa-clock-o"></i> Real Time <span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">
                      <li><a href="">Operation</a></li>
                      <li><a href="">Beliefs and Alerts</a></li>
                      <li><a href="">Drilling Optimization</a></li>
                    </ul>
                </ul>
              </div>
            </div>
          </div>
        </div>
        <!-- /side bar -->

        <div class="right_container">
          <div class="container-fluid">
            {% block content %} {% endblock %}
          </div>
        </div>
      </div>
    </div>

  </body>

As I was writing editing <a href=""> in base.html, my PyCharm IDE recognized contextual:bha argument , but it did not recognize pk=single_well_info.api argument, and that's the trouble. It seems to me that the context is not injected to base.html, when base.html is extended through views.py's template_name call

Upvotes: 1

Views: 1954

Answers (1)

Yassine Belmamoun
Yassine Belmamoun

Reputation: 420

You are probably extending base.html in each of your pages. So it is absolutely not dry to pass single_well_info_context in each of your view functions/classes.

You want to add single_well_info to your context to make it available in all your pages.

You need to open your settings.py and add a new context to your TEMPLATE_CONTEXT_PROCESSORS:

'context_processors': [
        ...
        # add a context processor
        'my_app.context_processor.single_well_info',

    ],

The single_well_info will be a fonction similar to:

def single_well_info(request):
    return {
        'api':"Hello",
    }

Upvotes: 1

Related Questions