Customizing Django Comments
The django.contrib.comments app, like all good apps, comes with many default templates so you can get them up and running in minutes. Unfortunately, by default they will not match your website, since they couldn't possibly know about your site's template structure. Fortunately, because of the way template loading works in Django, it's easy to extend these templates to match.
You may have noticed these lines in your settings.py:
1 2 3 4 | TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
)
|
When Django looks up a template, it will check these modules in order.
django.template.loaders.filesystem.load_template_source will check the directories you specified in the TEMPLATE_DIRS setting. If it doesn't find the associated template, it will move onto django.template.loaders.app_directories.load_template_source, which will look in the templates directories of apps you've listed in INSTALLED_APPS.
Barbara Shaurette recommended overriding any of the templates you wanted to customize by changing each to inherit from your base template, but there's a quicker way that lets you play dumb about how many templates there are (and any updates made to them).
Copy only the base.html template ([DJANGO_DIRECTORY]/contrib/comments/templates/comments/base.html) to your template directory and edit it so that instead of having the page markup included, like this:
templates/comments/base.html
1 2 3 4 5 6 7 8 9 10 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
|
... have it inherit from your base template (or whatever template you want):
1 2 3 4 | {% extends 'base.html' %}
{% block body %}
{% block content %}{% endblock %}
{% endblock %}
|
Using this method, by overriding just the top level template, you've customized all the comment templates, without even having to know what they are.
This method will of course work with any application. I just ran in to it specifically using the comments app.
- django
- February 5th, 2010 @ 7:43 p.m.
- 0 comments
