LoginSignup
17
25

More than 5 years have passed since last update.

PythonAnywhere上でDjangoを実行する

Last updated at Posted at 2015-06-28

各バージョン

  • Python: 3.4.0
  • Django: 1.8.2

python3用のvirtualenvを作成

terminalを開きます。

スクリーンショット 2015-06-28 17.16.36.png

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に公開する

アプリケーションの設定

ドメイン名を設定します。(無料プランだと変更できません。)
スクリーンショット 2015-06-28 17.41.11.png

使用するWebフレームワークを選択します。
Djangoがありますが、これを選択するとPython2.7系、Django1.6系になってしまうので Manual configuration を選択します。

スクリーンショット 2015-06-28 17.41.19.png

Pythonのバージョンを選択します。Python3.4一択です。

スクリーンショット 2015-06-28 17.41.25.png

とりあえずNextを押してしまいます。
スクリーンショット 2015-06-28 17.41.32.png

この段階で新しいdjexampleがPythonAnywhereで管理できるようになりました。

virtualenvの設定

Enter path to a virtualenv, if disired を押下すると自作したvirtualenvを使用するように設定できます。

スクリーンショット 2015-06-28 17.43.14.png

こんな感じ。
スクリーンショット 2015-06-28 17.44.32.png

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'

WSGIファイルはWEBからも書き換えられます。
スクリーンショット 2015-06-28 18.45.57.png

静的ファイルの設定を追加する

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 $ 

管理画面にアクセスする

アプリケーションをリスタート

スクリーンショット 2015-06-28 18.51.37.png

見てみる

http://takesxisximada.pythonanywhere.com/admin にアクセスする。

スクリーンショット 2015-06-28 18.52.43.png

やったー。

17
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
25