Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template tag to display Django version

What is the simplest way to create a Django template tag which displays the Django version on a template?

I want to put the following in a Django template and have it output the Django version (in my case, base.html):

{{ django_version }}

I know that the following Python code outputs the Django version in a shell, but am confused about where I should put this code and how I should call it from the template:

import django
print django.VERSION

UPDATE: I have tried the following in views.py, but nothing shows up in the template:

import django    
from django.template import loader, Context
from django.http import HttpResponse
from django.shortcuts import render_to_response

    def base(request):
        django_version = django.VERSION
        return render_to_response('base.html', {'django_version': django_version} )
like image 801
sgriffee Avatar asked Nov 23 '10 12:11

sgriffee


People also ask

What does {% endblock %} mean?

{% endblock %} </div> </body> </html> In this example, the {% block %} tags define four blocks that child templates can fill in. All the block tag does is tell the template enginetemplate engineTemplate system may refer to: Template processor, software designed to combine templates with a data model to produce result documents. Web template system, a system that allows web designers and developers work with web templates to automatically generate custom web pages.https://en.wikipedia.org › wiki › Template_systemTemplate system - Wikipedia that a child template may override those portions of the template.

What does {{ this }} mean in Django?

{{ foo }} - this is a placeholder in the template, for the variable foo that is passed to the template from a view. {% %} - when text is surrounded by these delimiters, it means that there is some special function or code running, and the result of that will be placed here.

What is with tag in Django template?

The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.

What does {% include %} do?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.


2 Answers

Figured this out (currently using Django 1.3) — I needed to append the function name 'django_version' to the TEMPLATE_CONTEXT_PROCESSORS tuple in settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (  
    # ...  
    'myproject.context_processors.django_version',  
)

context_processors.py (thanks to zsquare):

import django
def django_version(request):
    return { 'django_version': django.VERSION }

urls.py:

urlpatterns += patterns('django.views.generic.simple',
    (r'^$', 'direct_to_template', {'template': 'base.html'}),
)

Put the following in your templates, such as base.html:

{{ django_version }}
like image 200
sgriffee Avatar answered Sep 29 '22 21:09

sgriffee


You may want to use django.get_version() to get the django version in string, the django.VERSION returns a tuple.

>>> import django
>>> django.VERSION
(1, 11, 12, u'final', 0)
>>> django.get_version()
'1.11.12'
like image 20
andrei13 Avatar answered Sep 29 '22 20:09

andrei13