Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:31:16+00:00 2026-06-11T16:31:16+00:00

I’m learning Django now and have a simple question: how to handle additional page

  • 0

I’m learning Django now and have a simple question: how to handle additional page blocks (besides the main content block) in Django?

Let me explain what i mean. Assume we have a page “/news/view/12” which refers to “news.views.view” view function.
At this page i’m going to have blocks like navigation menu, recent blog posts, footer with some info and so on. In other words – these blocks will be placed at every page on the site.
And, of course, there will be the main content block with the content of the news item.

In this view function (“news.views.view”) i want to have a Python code exactly to handle the view of the one piece of news and nothing else:

def view(request, id):
    news_item = News.objects.get(pk=id)
    return render(request, 'news/view.html', {"item" : news_item})

So, how can i handle another blocks: fetch the data for them and then assign it to the template?

I mean the Python code somewhere in Django, not about templates and its inheritance (i know about it enough).

The most popular and convenient approaches are most appreciable 🙂


As i see you can’t understand me, let me show you solutions that i expect to hear from you.

The first solution is to use decorator functions:

@load_navigation
@load_recent_news
def view(request, id):
    news_item = News.objects.get(pk=id)
    return render(request, 'news/view.html', {"item" : news_item})

The second solution is to call python function directly in templates:

{% block recent_news %}
    {% call "news.views.recent_news" %} 
    {# this will call a python function which will return rendered template with recent news #}
{% endblock %}

And the third solution is to use django middleware.

But, to tell you the truth, i don’t like any of these approaches. I believe that django has a more convenient way to implement it.

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:31:17+00:00Added an answer on June 11, 2026 at 4:31 pm

    OK,according to your description, I think you can use customized templatetags for the purpose.
    Say you want to add recent news in the sidebar (And you have an app called news, while all news stored in a model called Article)

    news/templatetags/news.py

    from django import template
    from django.db.models import get_model
    register=template.Library();
    
    @register.simple_tag(takes_context=True)
    def get_latest_news(context,context_variable):
        context[context_variable]=get_model('News','Article').objects.order_by('-publish_date')[0,10]
        return ''
    

    base.html

    {% load news %}
    {% block sidebar %}
        {% get_latest_news 'latest' %}
        {% for one in latest %}
            <p>{{one}}</p>
        {% endfor %}
    {% endblock %}
    

    It’s better to start with simple_tag than the complete one as it’s much simpler and fits with most needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.