python:django:django_programming_memo

文書の過去の版を表示しています。


Django プログラミング メモ

 以下の内容については Django-1.2.3 を利用して記述している。

  1. 日本語を使用しても問題が発生しないように、先頭行に以下の記述を追加してファイルを UTF-8 にて保存しなおす。
    # -*- coding: utf-8 -*-
  2. 3行目にて os モジュールを import し、
    import os

    15行目付近で PROJECT_ROOT の取得を追加する。(各動作環境のパスの差異を吸収する)
    __file__ == '/private/django_proj/settings.py' の場合、'/private/django_proj/' を取得。
    __file__ == 'D:\My Projects\django_proj\settings.py' の場合、'D:\My Projects\django_proj' を取得。

    # プロジェクトのルートパスを取得
    PROJECT_ROOT = os.path.abspath(os.path.split(__file__)[0])
  3. 6行目に以下のコメントを追加して、デバッグとリリースの切り替えが簡単にできるようにしておく。(先頭 # を5, 6行目で移動するだけ)
    #DEBUG = False
  - データベースへの接続設定を行う。(以下は PostgreSQL の例)<code python>
DATABASES = {
    'default': {
        # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'ENGINE'    : 'django.db.backends.postgresql_psycopg2',
        'NAME'      : 'pydev_sample',   # Or path to database file if using sqlite3.
        'USER'      : 'pydev_sample',   # Not used with sqlite3.
        'PASSWORD'  : 'test123',        # 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. タイムゾーンをアジア/東京に設定する。
    TIME_ZONE = 'Asia/Tokyo'
  2. 言語コードを日本に設定する。
    LANGUAGE_CODE = 'ja'
  3. プロジェクトサイトのメディアルートを設定する。(以下ではプロジェクトルートの media ディレクトリを設定)
    '/private/django_proj/media' のような内容で設定される。
    MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
  4. プロジェクトサイトのメディア URL を設定する。(管理サイトのメディア URL と被らないように設定)
    MEDIA_URL = '/site_media/'
  5. 管理サイトのメディア URL を設定する。(プロジェクトサイトのメディア URL と被らないように設定)
    ADMIN_MEDIA_PREFIX = '/admin_media/'
  6. セキュリティキーは django-admin.py が生成したものを利用する。(他のプロジェクトのものをコピーして共有しないこと)
    SECRET_KEY = 'y*05eejz)j%+4&c-00i(crrgicz%2_pg+xonohqam0z0@7%f4('
  7. django.template.loaders.filesystem.Loader テンプレートローダが、プロジェクトルートの templates ディレクトリを検索するように設定する。
    TEMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        os.path.join(PROJECT_ROOT, 'templates'),
    )

 上記をすべて反映させたプロジェクト設定ファイルの内容は以下の通り。

1
# -*- coding: utf-8 -*-
# Django settings for pydev_sample project.
import os
 
DEBUG = True
#DEBUG = False
TEMPLATE_DEBUG = DEBUG
 
ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)
 
MANAGERS = ADMINS
 
# プロジェクトのルートパスを取得
PROJECT_ROOT = os.path.abspath(os.path.split(__file__)[0])
 
DATABASES = {
    'default': {
        # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'ENGINE'    : 'django.db.backends.postgresql_psycopg2',
        'NAME'      : 'pydev_sample',   # Or path to database file if using sqlite3.
        'USER'      : 'pydev_sample',   # Not used with sqlite3.
        'PASSWORD'  : 'test123',        # 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.
    }
}
 
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Asia/Tokyo'
 
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'ja'
 
SITE_ID = 1
 
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
 
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True
 
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
 
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/site_media/'
 
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin_media/'
 
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'y*05eejz)j%+4&c-00i(crrgicz%2_pg+xonohqam0z0@7%f4('
 
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)
 
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)
 
ROOT_URLCONF = 'pydev_sample.urls'
 
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_ROOT, 'templates'),
)
 
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)
  • python/django/django_programming_memo.1293079340.txt.gz
  • 最終更新: 2019/05/18 02:23
  • (外部編集)