Get the version of Django for application

2019-04-03 19:17发布

I am starting a new (actually very old) project which I know is in Django. I am getting lost knowing the exact version of Django it has been build upon. Is there a way I can know the version of Django my application is running?

5条回答
做自己的国王
2楼-- · 2019-04-03 19:33

This works in Django 1.7:

import django
django.VERSION

...which yields (on my machine):

(1, 7, 0, 'rc', 3)
查看更多
倾城 Initia
3楼-- · 2019-04-03 19:34

The only way is to take a guess. I would start by looking at the created date of the settings.py file (or other base project files)

Release dates for versions:

  • 1.0: September 2008. (?)
  • 1.1: July 29, 2009 [1]
  • 1.2: May 17, 2010 [2]
  • 1.3: March 23, 2011 [3]

Having in your urls.py:[4]

from django.conf.urls.defaults import *
from django.contrib import admin

or having an admin.py file in an application [5] suggests that it is a 1.0+ project.

Having in your urls.py: [6]

(r'^admin/', include(admin.site.urls)),

would suggest 1.1+.

Having in your settings.py file:

DATABASES = {
    'default': {
        'NAME': 'app_data',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'postgres_user',
        'PASSWORD': 's3krit'
    },
    'users': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': 'priv4te'
    }
}

would suggest 1.2+.

[1]: 1.1 release notes

[2]: 1.2 release notes

[3]: 1.3 release notes

[4]: Backwards Incompatible changes 0.96 > 1.0

[5]: Backwards Incompatible changes 0.96 > 1.0

[6]: Multiple databases

查看更多
唯我独甜
4楼-- · 2019-04-03 19:34

I see the answer accepted above and I think it's much easier. Maybe I'm missing something, but this is what I would do.

Open a python terminal that has the Django project on its path.

$ python
>>> import django
>>> print django.get_version()
0.97-pre-SVN-7668

That version number is strictly for illustration. Yours may differ, I hope.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-04-03 19:54

https://docs.djangoproject.com/en/1.7/intro/tutorial01/:

python -c "import django; print(django.get_version())"

which gets me the following:

1.7.1
查看更多
孤傲高冷的网名
6楼-- · 2019-04-03 19:56

You can guess based on the way settings.py is laid out. Your first hint would be from database settings. The old way prior to Django 1.2 was:

DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = os.path.join(BASE_DIR, 'db')             # Or path to database file if using sqlite3.
#DATABASE_USER = ''             # Not used with sqlite3.
#DATABASE_PASSWORD = ''         # Not used with sqlite3.
#DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
#DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

This method is still supported up to 1.3 but now causes Django to complain loudly about it being deprecated.

As of Django 1.2 the following format is used:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(PROJECT_DIR, 'mycms.db'),
    }
}

While this isn't definitive, it does at least give you a hint to whether your app was written either before or after Django 1.2.

Keep in mind is that an app written against an older version of Django should still work, but you'll likely get a lot of deprecation warnings on the console if your code is referencing stuff that has been deprecated or just moved around.

These warnings can usually safely be ignored in the short-term but you should definitely take time to silence them by updating your code to reference the features in their new home/format. The Django devs do a good job of doing the right thing as far as giving ample time and warning for older functionality to be properly migrated as time goes by.

查看更多
登录 后发表回答