Django项目从1.3.7升级到1.4.22

2017年来研究这个问题很奇怪。 毕竟,Django已经发展到1.11.3版本了。

不过,在工作中,什么奇怪的项目都会遇到。 孤当然也想直接升级到最新版本,可是这么多个大版本的跨度,足以令人望而生畏。

升级与报错

升级Django,本身倒是挺简单的。

$ pip install Django==1.4.22

问题在于,升级后启动报错。 以下错误,基本都是在settings.py中。

错误一:数据库配置

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

这个错误的详细Trace,没有必要看。

Django 1.4的数据库配置方式,有所改变。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

而原先1.3是:

DATABASE_ENGINE = 'sqlite3'     # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = ''              # 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.

代码不改,相当于数据库没有配,所以报错。

修改时,'ENGINE': 'django.db.backends.',这一行, 应该改为'ENGINE': 'django.db.backends.sqlite3',

直接填sqlite3是没用的,会报以下错误:

django.core.exceptions.ImproperlyConfigured: 'sqlite3' isn't an available database backend.

Try using django.db.backends.sqlite3 instead.

错误二:模板加载

Error importing template source loader django.template.loaders.filesystem.load_template_source: "'module' object has no attribute 'load_template_source'"

这个问题也不用看Trace,还是兼容性问题。

settings.py里的:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)

改成:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

错误三:ADMIN_MEDIA_PREFIX

旧版本如果使用了ADMIN_MEDIA_PREFIX来处理,那么在1.4以上是不支持的。

Starting in Django 1.4, the admin’s static files also follow this convention, to make the files easier to deploy. In previous versions of Django, it was also common to define an ADMIN_MEDIA_PREFIX setting to point to the URL where the admin’s static files live on a Web server. This setting has now been deprecated and replaced by the more general setting STATIC_URL. Django will now expect to find the admin static files under the URL <STATIC_URL>/admin/.

以上引用自django-contrib-admin。 新的做法,是使用django.contrib.staticfiles,详见《Managing static files | Django documentation 》。

INSTALLED_APPS中,增加一行:

    'django.contrib.staticfiles',

并且,使用STATIC*参数替换ADMIN_MEDIA_PREFIX与其它相关的做法。

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

在模板文件中,修改原先写死的静态文件URL。 例如,原先在ADMIN_MEDIA_PREFIX和本地目录下都有一个css/base.css,现在应该写为:

    <link rel="stylesheet" type="text/css" href="{% static 'admin/css/base.css' %}" />
    <link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}" />

另外,关于静态文件,可以参考另外一篇笔记《解决uWSGI里的Django静态文件丢失》。

错误四

Module "django.core.context_processors" does not define a "auth" callable request processor

这是auth模块改名所致。

TEMPLATE_CONTEXT_PROCESSORS中,原先的:

"django.core.context_processors.auth",

改为:

"django.contrib.auth.context_processors.auth",

详见《stackoverflow.com/questions/7470179》。

参考


相关笔记