I'm working on a Django application and I want to display the version of the application (such that people, who find bugs know the version of the application and can provide better bug reports).
Is there a universally accepted way to store version number in Django (I mean the version of my application, not Django) ?
I was looking for this exact same question, and found your question. The answer you accepted is not quite satisfactory to me.
I am working with django debugtoolbar, in there you can also show all versions of the apps used. I was wondering how to get the versions of my custom applications to show there as well.
Looking a bit further I found this question and answer: How to check the version of a installed application in Django in running time?
This answer however does not tell me where to put this __version__
So I looked in to an open application, which does show up in django toolbar. I looked in to the django restframework code, there I found out:
the version is put in the __init__.py
file
(see https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/init.py)
and it is put here as:
__version__ = '2.2.7' VERSION = __version__ # synonym
And after this, in his setup.py, he gets this version from this __init__.py
: see: https://github.com/tomchristie/django-rest-framework/blob/master/setup.py
like this:
import re def get_version(package): """ Return package version as listed in `__version__` in `init.py`. """ init_py = open(os.path.join(package, '__init__.py')).read() return re.match("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1) version = get_version('rest_framework')
When using buildout and zestreleaser:
By the way, I am using buildout and zest.releaser for building and versioning.
In this case, above is a bit different (but basically the same idea):
see http://zestreleaser.readthedocs.org/en/latest/versions.html#using-the-version-number-in-setup-py-and-as-version
The version in setup.py is automatically numbered by setup.py, so in __init__.py
you do:
import pkg_resources __version__ = pkg_resources.get_distribution("fill in yourpackage name").version VERSION = __version__ # synonym
There are many places where you can store your app version number and a few methods that allow you to show it in django templates. A lot depends on the release tool you're using and your own preferences.
Below is the approach I'm using in my current project.
I'm storing the app version number in the version.txt file. It's one of the locations the zest.releaser release tool (that I'm using) takes into account while doing a release.
The whole content of version.txt is just the app version number, for example: 1.0.1.dev0
... with open(version_file_path) as v_file: APP_VERSION_NUMBER = v_file.read() ...
This paragraph and the following ownes are based on the wonderful answer by bcchun to Can I access constants in settings.py from templates in Django?
A custom context processor will allow you to add the app version number to the context of every rendered template. You won't have to add it manually every time you render a template (and usually you'll want to have the version number somewhere in the footer of every page).
Create context_processors.py file in your app directory:
from django.conf import settings def selected_settings(request): # return the version value as a dictionary # you may add other values here as well return {'APP_VERSION_NUMBER': settings.APP_VERSION_NUMBER}
TEMPLATES = [{ ... 'OPTIONS': { 'context_processors': [ ... 'your_app.context_processors.selected_settings' ], }, }]
RequestContext
or render
in viewsRequestContext and render
populate the context with variables supplied by context_processors you set in settings.py.
Example:
def some_view(request): return render(request, 'content.html')
... <div>{% trans 'App version' %}:{{APP_VERSION_NUMBER}}</div> ....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With