Python Forum
Django Two blocks of dynamic content on one page
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django Two blocks of dynamic content on one page
#1
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.
Reply
#2
This seems like a Javascript question more than Python - you don't even mention the Python framework you're using.
Reply
#3
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.
Reply
#4
Can anyone help with this or is it just too much of a mess?
Reply
#5
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})
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scraping the page without distorting content oleglpts 5 2,486 Dec-16-2021, 05:08 PM
Last Post: oleglpts
  <title> django page title dynamic and other field (not working) lemonred 1 2,102 Nov-04-2021, 08:50 PM
Last Post: lemonred
  Web scraping cookie in URL blocks selenium Alex06 2 2,439 Jan-10-2021, 01:43 PM
Last Post: Alex06
  Getting a list in dynamic page probottpric 1 1,785 Oct-12-2020, 01:11 AM
Last Post: Larz60+
  [Django] css file is not applying to the page SheeppOSU 1 3,069 May-09-2020, 06:54 AM
Last Post: menator01
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 3,626 Mar-19-2020, 06:13 PM
Last Post: apollo
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,306 Jun-30-2019, 12:21 AM
Last Post: scidam
  Sorting getting off, when I switch page Django 1.11 m0ntecr1st0 0 1,976 Feb-12-2019, 06:40 PM
Last Post: m0ntecr1st0
  Django-cms link to a page Alkatron 4 8,247 Apr-06-2018, 10:58 AM
Last Post: Alkatron
  How to create dynamic webscraper in Django using BeautifulSoup Prince_Bhatia 1 6,163 Jan-26-2018, 02:07 PM
Last Post: frostbite

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020