Mezzanine4上で、Mezzanine3系のsettings.py等を使用していると、
警告がやたら出力されるので、
Mezzanine4の構成にファイル類を移動、修正してみました結果をメモします。


環境情報

  • OS
    CentOS release 6.7 (Final)

  • Python Version
    Python 2.7.8

  • Package (必要そうなものだけ抜粋)
    Django (1.9.6)
    Mezzanine (4.1.0)


前提

警告が出てますが、特に3系のsettings.pyでも、
動作上は問題ないと思われます。
plugin追加とかやるときになるので、構造を合わせようかと。


Version3 Version4でディレクトリ構造が違う

  • ディレクトリ構造
    以下、コマンドでプロジェクト作成した際の構造が違います。
    Version4は、djangoのいつものapplicationの構造になっていました。

mezzanine-project myproject

  • Version3のディレクトリ構造(コマンドを叩いたわけでないです。自プロジェクト構造からの推測)

    # プロジェクトディレクトリ直下
    __init__.py
    deploy
    fabfile.py
    local_settings.py
    manage.py
    requirements.txt
    settings.py
    static
    urls.py
    wsgi.py
    
    # deployディレクトリ配下
    crontab
    gunicorn.conf.py.template
    local_settings.py.template
    nginx.conf
    supervisor.conf
    

  • Version4のディレクトリ構造

    # プロジェクトディレクトリ直下
    __init__.py
    deploy
    fabfile.py
    manage.py
    myproject
    requirements.txt
    
    # deployディレクトリ配下
    crontab.template
    gunicorn.conf.py.template
    local_settings.py.template
    nginx.conf.template
    supervisor.conf.template
    
    # myprojectディレクトリ配下
    __init__.py
    local_settings.py
    settings.py
    urls.py
    wsgi.py
    
    Version3だと、プロジェクト直下に配布されていたlocal_settings.py,settings.py,urls.py,wsgi.pyが、
    Verison4だと、1階層深くなり配置されています。


ファイルごとの差分 manage.py

Version4をベースに、project名を自プロジェクトに変更すれば、良さそうです。
変更点は以下の通りです。

  • from __future__ import absolute_import, unicode_literals削除されている。

  • real_project_nameいうメソッドが新規追加されている。

  • コメントが削除され、ファイルが小さくなっている。

  • Version4のmanage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":

    from mezzanine.utils.conf import real_project_name
    settings_module = "%s.settings" % real_project_name("your_project_name")
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)


ファイルごとの差分 urls.py

ここは、それなりに編集を加えているので、Version3ベースで4の変更点を取り込みます。 変更点は以下の通りです。

  • from django.views.i18n import set_language追加されている。

  • urlpatterns = i18n_patterns("",の、("",削除されている。

  • urlpatterns += patterns('',が、urlpatterns += [変更

RemovedInDjango110Warning関係する箇所に修正が入っています。


ファイルごとの差分 settings.py

ここも、それなりに編集を加えているので、Version3ベースで4の変更点を取り込みます。
変更点は以下の通りです。

  • import os追加

  • from django import VERSION as DJANGO_VERSION追加

  • from django.utils.translation import ugettext_lazy as _追加

  • SECRET_KEY削除されている。

  • NEVERCACHE_KEY削除されている。

  • USE_MODELTRANSLATION = False追加

  • USE_SOUTH = True削除されている。

  • PROJECT_ROOTパス変数が追加、変更されている。

    PROJECT_APP_PATH = os.path.dirname(os.path.abspath(__file__))
    PROJECT_APP = os.path.basename(PROJECT_APP_PATH)
    PROJECT_ROOT = BASE_DIR = os.path.dirname(PROJECT_APP_PATH)
    

  • PROJECT_DIRNAME削除

  • ROOT_URLCONFVALUE値の変更

  • DEPLOY SETTINGSコメント部の削除

##################
# LOCAL SETTINGS #
##################

f = os.path.join(PROJECT_APP_PATH, "local_settings.py")
if os.path.exists(f):
    import sys
    import imp
    module_name = "%s.local_settings" % PROJECT_APP
    module = imp.new_module(module_name)
    module.__file__ = f
    sys.modules[module_name] = module
    exec(open(f, "rb").read())

ファイルごとの差分 wsgi.py

これはVersion4ベースでよさそうです。

"""
WSGI config for myproject project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application
from mezzanine.utils.conf import real_project_name

os.environ.setdefault("DJANGO_SETTINGS_MODULE",
                      "%s.settings" % real_project_name("your_project_name"))

application = get_wsgi_application()

ファイルごとの差分 local_settings.py

これもVersion4ベースでよさそうです。
Version3よりも、Version4での記述が増えただけ
まるっとコピー&ペーストして、不要なところをコメントアウトします。

# This file is exec'd from settings.py, so it has access to and can
# modify all the variables in settings.py.

# If this file is changed in development, the development server will
# have to be manually restarted because changes will not be noticed
# immediately.

DEBUG = True

# Make these unique, and don't share it with anybody.
SECRET_KEY = "xxxxxxxx"
NEVERCACHE_KEY = "xxxxxxxxx"

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

###################
# DEPLOY SETTINGS #
###################

# Domains for public site
# ALLOWED_HOSTS = [""]

# These settings are used by the default fabfile.py provided.
# Check fabfile.py for defaults.

# FABRIC = {
#     "DEPLOY_TOOL": "rsync",  # Deploy with "git", "hg", or "rsync"
#     "SSH_USER": "",  # VPS SSH username
#     "HOSTS": [""],  # The IP address of your VPS
#     "DOMAINS": ALLOWED_HOSTS,  # Edit domains in ALLOWED_HOSTS
#     "REQUIREMENTS_PATH": "requirements.txt",  # Project's pip requirements
#     "LOCALE": "en_US.UTF-8",  # Should end with ".UTF-8"
#     "DB_PASS": "",  # Live database password
#     "ADMIN_PASS": "",  # Live admin user password
#     "SECRET_KEY": SECRET_KEY,
#     "NEVERCACHE_KEY": NEVERCACHE_KEY,
# }


ファイル名称を揃えておく

Version3とVersion4でファイル名称の異なるものがあるので、揃えておきます。

cd ${PROJECT_ROOT}/deploy
mv crontab crontab.template
mv nginx.conf nginx.conf.template
mv supervisor.conf supervisor.conf.template

ファイル編集はこれで終わりです。


WSGIScriptAliasの修正

Apacheを再起動して、動作確認したところ、
以下のエラーでページが表示されませんでした。

[Sun May 08 01:23:27 2016] [error] [client xxx.xxx.xxx.xxx] Target WSGI script not found or unable to stat: ${プロジェクトルート}/wsgi.py

ApacheのWSGIScriptAliasの修正をしていなかったのでそちらも修正します。 * 修正箇所

WSGIScriptAlias / ${プロジェクトルート}/wsgi.py process-group=blog application-group=%{GLOBAL}

Apacheを再々起動後、無事画面が表示されました。
以上です。

コメント