各バージョン
- Python: 3.4.0
- Django: 1.8.2
python3用のvirtualenvを作成
terminalを開きます。
terminarl上でvritualenvを作成します。
08:20 ~ $ virtualenv --python="python3" env
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in env/bin/python3
Also creating executable in env/bin/python
Installing setuptools, pip, wheel...done.
08:21 ~ $
virtualenvをactivateします。
08:21 ~ $ source env/bin/activate
(env)08:21 ~ $
djangoをinstall
pip install django
でdjangoをinstallします。
(env)08:21 ~ $ pip install django
Collecting django
Installing collected packages: django
Successfully installed django-1.8.2
projectを作る
python manage.py startproject PROJECTNAME
でprojectを作成します。
(env)08:27 ~ $ django-admin startproject djexample
(env)08:27 ~ $
syncdbする
作成したprojectのディレクトリに移動します。
(env)08:34 ~ $ cd djexample/
とりあえずsyncdbします。
(env)08:35 ~/djexample $ python manage.py syncdb
/home/TakesxiSximada/env/lib/python3.4/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The sync
db command will be removed in Django 1.9
warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: auth, admin, contenttypes, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
No migrations to apply.
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'takesxisximada'): ADMINISTORATOR_NAME
Email address: your.email@mail.address
Password:
Password (again):
Superuser created successfully.
(env)08:35 ~/djexample $
Webに公開する
アプリケーションの設定
使用するWebフレームワークを選択します。
Djangoがありますが、これを選択するとPython2.7系、Django1.6系になってしまうので Manual configuration
を選択します。
Pythonのバージョンを選択します。Python3.4一択です。
この段階で新しいdjexampleがPythonAnywhereで管理できるようになりました。
virtualenvの設定
Enter path to a virtualenv, if disired
を押下すると自作したvirtualenvを使用するように設定できます。
wsgiファイルの設定
読み込まれるwsgiファイルは固定になるようなので、/var/www
配下にあるwsgiファイルを差し替えます。
とりあえず古いファイルをバックアップします。
(env)08:55 ~/djexample $ cp /var/www/takesxisximada_pythonanywhere_com_wsgi.py /var/www/takesxisximada_pythonanywhere_com_wsgi.py.old
(env)08:55 ~/djexample $
djexampleのwsgi.pyをもともと/var/www配下にあったファイルと同じ名前でcopyします。
(env)08:55 ~/djexample $ cp djexample/wsgi.py /var/www/takesxisximada_pythonanywhere_com_wsgi.py
(env)08:56 ~/djexample $
そして次のように書き換えます。
"""
WSGI config for djexample 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.8/howto/deployment/wsgi/
"""
import os
import sys
from django.core.wsgi import get_wsgi_application
path = '/home/TakesxiSximada/djexample'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'djexample.settings'
application = get_wsgi_application()
実際には以下を追加しました。
path = '/home/TakesxiSximada/djexample'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'djexample.settings'
静的ファイルの設定を追加する
settings.pyにSTATIC_ROOTを追加
(env)09:47 ~/djexample $ tail djexample/settings.py
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urls.pyに静的ファイルの設定を追加
(env)09:48 ~/djexample $ cat djexample/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
collectstaticする
static fileをcllectstaticで回収します。
(env)09:50 ~/djexample $ python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
/home/TakesxiSximada/djexample/static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
~省略~
(env)09:50 ~/djexample $
管理画面にアクセスする
アプリケーションをリスタート
見てみる
http://takesxisximada.pythonanywhere.com/admin にアクセスする。
やったー。