Django 1.3 の設定を少し
後は Django の設定の話。
Django のドキュメントにチュートリアルがあるのだけど、Django 1.3 だと settings.py の中身がちょっと違ってたりするのでメモ。
まずは settings.py のデータベースの設定。
[paraches@localhost myApps]$ cat settings.py <省略> import os DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': os.path.realpath(os.path.dirname(__file__)) + os.sep + 'db.sqlite3', # 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. } } <省略>
データベースは sqlite3 が「ENGINE」と「NAME」を設定するだけで良いので簡単。
データベースのファイル db.sqlite3 は、プロジェクトの myApps フォルダに作る設定にした。
で、suncdb すると…
[paraches@localhost myApps]$ python manage.py syncdb Traceback (most recent call last): <省略> sqlite3.OperationalError: unable to open database file
はい、エラー。
今回の場合、myApps に(たぶん apache さん)が書き込めないとダメ。今は 755 なので 757 にして再度トライ。
[paraches@localhost myApps]$ sudo chmod 757 . [paraches@localhost myApps]$ python manage.py syncdb Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_user_permissions Creating table auth_user_groups Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): yes Username (Leave blank to use 'paraches'): E-mail address: hoge@hoge.com Password: Password (again): Superuser created successfully. Installing custom SQL ... Installing indexes ... No fixtures found. [paraches@localhost myApps]$
完了。
で、チュートリアルに従って polls アプリを作ってみる。
[paraches@localhost myApps]$ python manage.py startapp polls [paraches@localhost myApps]$ ls __init__.py db.sqlite3 myDjango.wsgi settings.py urls.py __init__.pyc manage.py polls settings.pyc urls.pyc [paraches@localhost myApps]$
ちゃんとできてる。
次に admin 関係のメモ。
チュートリアルを進めて、settings.py のアプリケーションの部分と url.py の admin を有効にする。
[paraches@localhost myApps]$ cat settings.py <省略> INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'myApps.polls' ) <省略>
[paraches@localhost myApps]$ cat urls.py from django.conf.urls.defaults import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'myApps.views.home', name='home'), # url(r'^myApps/', include('myApps.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), ) [paraches@localhost myApps]$
そしてブラウザでアクセス!
はい、エラー。
db.sqlite3 が 644 なのでまた apache さんが書き込めない。db.sqlite3 を apache さんの物にしちゃえば良い。
あと、db.sqlite3 が apache の物になったら、myApps は誰でも書ける状態じゃなくても良いので元に戻しておく。
[paraches@localhost myApps]$ sudo chown apache:apache db.sqlite3 [paraches@localhost myApps]$ cd .. [paraches@localhost django]$ sudo chmod 755 myApps/
今度こそ!
はい、アクセスできたけど、css が読み込めてない。
admin の media フォルダへのパスを通さなきゃダメ。
前の Django 1.2.3 の時は settings.py での設定が
ADMIN_MEDIA_PREFIX = '/media/'
となっていたので
[paraches@centos html]$ sudo ln -s /usr/local/lib/python2.6/site-packages/Django-1.2.3-py2.6.egg/django/contrib/admin/media/ media
でいけたけど、Django 1.3 は static とか出てきてちょっと違う。
settings.py での設定が
ADMIN_MEDIA_PREFIX = '/static/admin/'
となっているので、/var/www/html/ に static フォルダを作って、そこからリンクを張る。
[paraches@localhost html]$ sudo mkdir static [paraches@localhost html]$ cd static/ [paraches@localhost static]$ sudo ln -s /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/ admin
これでちゃんと css が読み込めて綺麗に表示される。
取り敢えず以上で設定はできたかな。
パーミション関係で出てくるエラーが面倒だけど、なんとかこの状態で動いてる。
後は自分のアプリケーションを作って動かすだけだけど、テンプレートの位置や static だの media の位置がまた面倒な気がする。
その辺りはまた時間があったら…