1

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

Eric Kim
  • 2,493
  • 6
  • 33
  • 69
  • Add `block` in base.html and add context inside the block – seuling Jul 20 '18 at 06:09
  • @seuling I don't understand what you are saying, because **base.html** already has a block, and **contextual_main.html** extends on the block inside **base.html**. The context is already injected inside the block, but the context does not work outside of the block. – Eric Kim Jul 20 '18 at 06:16
  • So you can add other block inside your navigation. Or you can add your own context_processor for sending context to base.html - but you don't have to pass those data in other page, right? – seuling Jul 20 '18 at 06:17
  • It should just work. What do you mean by "_it won't work_"? Post the relevant portions from your `base.html` please. – Selcuk Jul 20 '18 at 06:36
  • @Selcuk please check the edit – Eric Kim Jul 20 '18 at 06:44
  • Read https://stackoverflow.com/questions/34902707/how-can-i-pass-data-to-django-layouts-like-base-html-without-having-to-provi#34903331 – Márcio Moreira Oct 18 '18 at 12:22

1 Answers1

1

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",
    }
  • One complication I have with this method is that, `single_well_info` should be the same object as in **views.py**'s `ContextualMain_DetailView`. How do I make sure that `single_well_info` in **context_processor** is the exact same object instance that I passed into **contextual_main.html*? – Eric Kim Jul 20 '18 at 07:07
  • You need to adapt your function **def single_well_info(request):** based on the request or a new parameter. Keep in mind that we don't have the big picture of your code in order to help you. – Yassine Belmamoun Jul 20 '18 at 07:21