Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generic Template with Extra Context
<!doctype html>
<html>
<head>
<title>{{page_name}}</title>
</head>
<body>
<h1>{{page_name}}</h1>
{% block content %}
{% endblock content %}
</body>
</html>
{% extends "base.html" %}
{% block content %}
<p>Welcome to my home page.</p>
{% endblock content %}
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
url(r'^$',
direct_to_template,
{'template': 'home.html',
'extra_context': {'page_name': 'Home'}},
name='home'),
url(r'^other$',
direct_to_template,
{'template': 'other.html',
'extra_context': {'page_name': 'Other Page'}},
name='other'),
)
{% extends "base.html" %}
{% block content %}
<p>This is some other page in my site that also extends the base template.</p>
{% endblock content %}
from django.conf.urls.defaults import patterns, include, url
from views import ExtraContextTemplateView
urlpatterns = patterns('',
url(r'^$',
ExtraContextTemplateView.as_view(template_name='home.html',
extra_context={'page_name': 'Home'}),
name='home'),
url(r'^other$',
ExtraContextTemplateView.as_view(template_name='other.html',
extra_context={'page_name': 'Other Page'}),
name='other'),
)
from django.views.generic import TemplateView
class ExtraContextTemplateView(TemplateView):
extra_context = None
def get_context_data(self, *args, **kwargs):
context = super(ExtraContextTemplateView, self).get_context_data(*args, **kwargs)
if self.extra_context:
context.update(self.extra_context)
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.