Sandeep
Sandeep

Reputation: 21144

django base template date rendering

I am very new to django and I have a simple problem. Hope that you will help me solve this. I have a base template like this;

<!doctype html>
<html>
<head>
<title>{% block title %} {% endblock %}</title>
<link type="text/css" rel ="stylesheet" href="style.css" />
<script type="text/javascript" src="jquery.js"> </script>
</head>
<body>
{% block navmenu %} 
{% endblock %}
{% block content %}
{% endblock %}
{% block footer %}
{% endblock  %}
</body>
</html>

While this template is being extended by some intermediate template which has structure like this,

{% extends "base.html" %}
{% block navmenu %}
<ul>
<li>Home</li>
<li>Programming</li>
<li>About</li>
<li>Contact</li>
</ul>
{% endblock %}
{% block footer %}
<h4>Copyright &#169, {{year}} Mel Gibson </h4>
{% endblock %}

Both of this template look very straight forward, while my view renders different template that extends this intermediate template. I have a simple problem here. The block called footer has a variable called year. This should be constant through out the page, so if I keep on passing this variable in context, it would be redundancy, what is the easiest way to render this date, so that it is passed only once through out the code ? Can I import the datetime object in the html file itself and build year field within the same template to display.

Upvotes: 0

Views: 838

Answers (4)

Carlo Pires
Carlo Pires

Reputation: 4916

Django has a supported solution to this use case: RequestContext

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599470

This is what context processors are for.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409136

Let me see if I get this straight: The year should always be the current year? Not just a static text, because then you would have written it instead, am I right?

If you don't want to pass it as a variable, then you could create a custom template tag to use instead.

Or you could create a base view class that all other view classes inherits, and which adds commonly used template variables, like the year.

Upvotes: 1

Related Questions