Python Forum

Full Version: Django Two blocks of dynamic content on one page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can anyone explain how this is done, I can get one block of dynamic content but can't get a second block to show. Whilst I have two views, I was of the opinion that it's one view per page. Basically, I have two database tables one for the main feed, and a second one for a side bar, but I can't get side bar to show up as the url only points to the one extended page. What is the proper way to add other dynamic content please.
This seems like a Javascript question more than Python - you don't even mention the Python framework you're using.
Apologies, I've been going out of my mind with this as I am unable to find any information on something that is just a standard practice in websites now. I am using Django and I am still very much stuck on this. I can get this first page here to show up as a separate web page, so know it works, but I can't get it to show up where I need it as the side bar to my main page, here is the part I am trying to include?

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    {% for category in categories %}
    <li><span class="caret">{{ category.name }}</span>
      {% if category.link_set %}
      <ul class="nested">
        {% for link in category.link_set.all %}
        <li><a href="{{ link.url }}" target="_blank">{{ link.name }}</a></li>
        {% endfor %}
      </ul>
      {% else %}
      :without children links
      {% endif %}
    </li>
    {% endfor %}
  </body>
</html>
and here is a stripped down version of the actual page that I want to put it in:
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
  <head>
    <title>Platform Control</title>
    <meta charset="utf-8">
    <meta http-equiv="refresh" content="300; URL=http://10.88.58.95">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="{% static "css/microblog/style.css" %}"/>
  </head>

  <body>
      <div class="container">    
          <div class="column left">
            <ul id="myUL">
              <div class="links">
                  {% include 'url_tree/category_list.html' %}      <-- Trying to add content here
              </div>
            </ul>
          </div>

          <div class="column middle">
            {% block center-block %}
            {% endblock %}
          </div>
       <div class="column right">
           <h5>Other links</h5>
       </div>
    </div>
  </body>
  </html>
I've been stuck on this for about a month now and have only found one stack exchange post that came up, but was for a different issue. All tutorials I have done and found about django show you how to add one bit of dynamic code, which is the {% block center-block %} this bit works, but getting a second bit of dynamic content is not explained anywhere that I can find. Please help, I have no idea where to go from here as I don't know any django developers, so am completely stuck. How do other people get dynamic content throughout their pages?

EDIT: just to add a little clarification, I can't get any text to show up, using the include function, be it the html I have posted or a simple hello world.
Can anyone help with this or is it just too much of a mess?
This sounds like something that can be handled at the view-level, instead of the template-level. ie: you shouldn't want to start a new web request and paste it's output into the middle of your template. Instead, the view should just return both objects that are relevant to the template.

Something like:
# this isn't a view, it only gets data that's used in multiple different views
def get_categories():
    return Categories.all() # or whatever

def index(request):
    categories = get_categories()
    return render(request, "index.html", {"categories": categories})
Hi,

@iFunKtion: I think you have a wrong impression on what's going on. include does what the name says - it includes another template into the current one. _Both_ template share the same context, thus you need to pass _all_ context data used by the main template and the included template to the main template. See also the Django documentation of include: https://docs.djangoproject.com/en/2.2/re...s/#include

Regards, noisefloor