Daniel Quinn
Daniel Quinn

Reputation: 6428

How to get something to appear on every page in Django?

I'm curious as to the best-practise way to handle having something appear on every page, or on a number of pages without having to assign the data manually to every page like so:

# views.py

def page0(request):
    return render_to_response(
        "core/index.html",
        {
            "locality": getCityForm(request.user),
        },
        context_instance=RequestContext(request)
    )

def page1(request):
    return render_to_response(
        "core/index.html",
        {
            "locality": getCityForm(request.user),
        },
        context_instance=RequestContext(request)
    )
...
def page9(request):
    return render_to_response(
        "core/index.html",
        {
            "locality": getCityForm(request.user),
        },
        context_instance=RequestContext(request)
    )

Now I can think of a few ways to do this, including writing my own Context or maybe some middleware, and of course, copy/pasting this locality assignment on every page... I'm just not sure of the best way to do this. I'm pretty sure that it's not that last one though.

Upvotes: 3

Views: 2761

Answers (3)

bigjust
bigjust

Reputation: 299

use inheritance in the templating engine:

have a base.html that includes the common code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" href="style.css" />
<title>{% block title %}My amazing site{% endblock %}</title>
</head>

<body>
<div id="sidebar">
    {% block sidebar %}
    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/blog/">Blog</a></li>
    </ul>
    {% endblock %}
</div>

<div id="content">
    {% block content %}{% endblock %}
</div>
</body>
</html>

then in each page that needs that common code, simply:

{% extends "base.html" %}
{% block title %}My amazing blog{% endblock %}
{% block content %}
{% for entry in blog_entries %}
<h2>{{ entry.title }}</h2>
<p>{{ entry.body }}</p>
{% endfor %}
{% endblock %}

http://docs.djangoproject.com/en/dev/topics/templates/#id1

this combined with context processessing will eliminate a lot of duplicate code.

Upvotes: 4

Alex Martelli
Alex Martelli

Reputation: 882591

To do exactly what you request, I'd simply define a function:

def foobar(req):
    return render_to_response(
        "core/index.html",
        {
            "locality": getCityForm(req.user),
        },
        context_instance=RequestContext(req)
    )

put it in some module myfun, and return myfun.foobar(request) wherever needed. You're likely to want a few more arguments, but as long as it stays about this simple, it's simpler that defining middleware, using OOP, &c.

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375932

You want a context processor. The data they generate is included in every context created as a RequestContext. They are perfect for this.

Combined with base templates that show common things, you can get rid of lots of need for copying and pasting code.

Upvotes: 16

Related Questions