2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

djangoRESTAPI作成

Last updated at Posted at 2017-12-22

DjangoでRESTApiの実装したときの忘備録.

基本的には,以下を参考に実装.
django : django
djangoRESTAPI : Django REST Frameworkを使って爆速でAPIを実装する

GitHubにコードは置いてます.

インストール

pip install django django-filter djangorestframework

参考: Home - Django REST framework

Djangoプロジェクトの作成

django-admin startproject mySite

DjangoをApacheにデプロイ

環境

Ubuntu 16.04 LTS
Apache 2.4.18
Django 1.11.7

ディレクトリ構成

/var  
┗ www/
    ┣ html (今回は関係なし)
    ┗ djangoRESTApi/  (GitHub Repository Directory)  
        ┗ djangoREST/ (Project Directory)
            ┝ manage.py
            ┝ db.sqlite3
            ┝ blog/         (Application Directory)
            ┝ static/       (Static Files Directory)
            ┗ djangoREST/   (Application Directory)

Django

ホスト名変更

localhostでは,他のPCからアクセス可能にするためにホスト名をIPアドレスに変更.
下記のように,ALLOWED_HOSTSにサーバのIPアドレスを設定.

djangoREST/setting.py
ALLOWED_HOSTS =['132.423.423.42']

静的(Static)ファイルの設定

今回は,Django REST Framework を実装しているため,Python3のライブラリのPathにレイアウト用のBootStrapやjQueryが配置されている.なので,パスを通す.

DJANGO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(DJANGO_ROOT)
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
  • DJANGO_ROOT : Djangoのルートディレクトリ
  • PROJECT_ROOT : Djangoプロジェクトのルートディレクトリ (ex. /var/www/djangoRESTApi/djangoREST/)
  • STATIC_ROOT : Djangoの静的ファイルの格納場所 (ex. /var/www/djangoRESTApi/djangoREST/static/)

以下のコマンドで,STATIC_ROOTの場所にアプリで使用するstaticファイルが集められる.

python manage.py collectstatic

これで,staticにcssやjsファイルが保存される.

Apache

WSGIの使用を宣言

以下を追記

apache2.conf
WSGIScriptAlias /django /var/www/djangoRESTApi/djangoREST/djangoREST/wsgi.py  
  • WSGIScriptAlias : スキーマとdjangoAppを紐付け (http://localhost/django でdjangoREST のアプリケーションに接続する)

WSGIのPython設定

WsgiでPythonをApacheに伝えるために,Python関連のパスを定義する必要がある.
/etc/apache2/apache2.confに追記

apache2.conf
WSGIPythonPath /var/www/djangoRESTApi/djangoREST
WSGIPythonPath /usr/local/lib/python3.5/dist-packages
WSGIPythonHome /usr/bin/python
Alias /static /var/www/djangoRESTApi/djangoREST/static
  • WSGIPythonPath : Pythonモジュールのパス設定 (1行目: djangoAppのパス,2行目: Djangoのライブラリパス)
  • WSGIPythonHome : Python本体のライブラリパス

参考:
Django、静的ファイルのデプロイ(Apache) - naritoブログ
Apache と mod_wsgi 環境で Django を使う方法 — Django 1.4 documentation

各ディレクトリの設定

続けて /etc/apache2/apache2.confに追記

apache2.conf
<Directory /var/www/djangoRESTApi/djangoREST/djangoREST>
<Files wsgi.py>
        Require all granted
</Files>
</Directory>

<Directory /var/www/djangoRESTApi/djangoREST/static/rest_framework>
        Options Indexes FollowSymLinks
        Require all granted
</Directory>
  • 1つ目のディレクトリ (/var/www/djangoRESTApi/djangoREST/djangoREST)
    • wsgi.pyへのアクセスを有効化
    • Apache2.4以降から,Require all grantedで有効化
  • 2つ目のディレクトリ (/var/www/djangoRESTApi/djangoREST/static/rest_framework)
    • REST frameworkで使用されているcss,jsファイルをアクセス可能にする.
    • Options Indexes FollowSymLinksでシンボリックリンクを許可

未解決問題

MAMPでDjangoをデプロイ

以下の環境で同様のことを,自分の環境でするとダメでした・・・

  • Mac
  • MAMP
  • Python3.5 (pyenvで構築)

mod_wsgi Documentを見るとVirtual Environment and Python Versionの項に,Virtual 環境ではwsgiが使えないと言う記述があったので,今回は断念しました.

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?