0
0

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.

Win10環境でDjango2のREST APIを試してみる

Last updated at Posted at 2018-12-27

前提

Windows10にPythonやDjango2をインストールする記事はやたらあるので省略

最終的にはminishift上でDjango2のREST APIを試したくて試行錯誤してみます。
minishiftで作る前にDjango2のソースを作成しておきます。

環境

Django2のインストール

C:\Users\tak>pip --version
pip 18.1 from c:\python\python37\lib\site-packages\pip (python 3.7)

C:\Users\tak>pip install django==2.1.4
Collecting django==2.1.4
  Using cached https://files.pythonhosted.org/packages/fd/9a/0c028ea0fe4f5803dda1a7afabeed958d0c8b79b0fe762ffbf728db3b90d/Django-2.1.4-py3-none-any.whl
Collecting pytz (from django==2.1.4)
  Downloading https://files.pythonhosted.org/packages/f8/0e/2365ddc010afb3d79147f1dd544e5ee24bf4ece58ab99b16fbb465ce6dc0/pytz-2018.7-py2.py3-none-any.whl (506kB)
    100% |████████████████████████████████| 512kB 1.1MB/s
Installing collected packages: pytz, django
Successfully installed django-2.1.4 pytz-2018.7

同様に必要なモジュールをインストールしていきます。

C:\Users\tak>pip install djangorestframework
C:\Users\tak>pip install django-filter
:

Local環境でDjango2の超簡単なREST APIを作成

eclipseを起動し新しいPyDev Djangoプロジェクト「smaple」を作成します。

01.png

APPを作成します。

c:\>cd rest\sample
c:\rest\sample>python manage.py startapp restapi

02.png

自前のAPPを登録します。

sample/setting.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',    # 追加
    'restapi',           # 追加
]

TIMEZONEの設定を行います。

sample/setting.py
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'ja'       # 修正
TIME_ZONE = 'Asia/Tokyo'   # 修正

末尾にrest frameworkの設定を追加します。
まあ今回のには必要ないんですけどね(笑)

sample/setting.py
# rest framework Internationalization
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10
}

urlを登録します。

sample/urls.py
"""sample URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from restapi import views                                      # 追加

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', views.current_time, name='current_time'),     # 追加
]

今回は簡単に現在時刻をJSON形式にするだけの簡単なものにしました。

restapi/views.py
from datetime import datetime
from django.http import JsonResponse
from rest_framework import status
from rest_framework.decorators import api_view

# Create your views here.

@api_view(['GET'])
def current_time(request):
    ret = {'now':str(datetime.now())}
    return JsonResponse(ret, status=status.HTTP_200_OK)

テスト用コードを書きますが、ここも横着してリターンコード(HTTP_200_OK)の確認のみとしてます。

restapi/test.py
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

# Create your tests here.
class restapiTest(APITestCase):
    def test_current_time(self):
        url = reverse('current_time')
        response = self.client.get(url, '')

        self.assertEqual(response.status_code, status.HTTP_200_OK)

一応、makemigrations & migrate

c:\rest\sample>python manage.py makemigrations
No changes detected

c:\rest\sample>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

でデバックモードで実行しブラウザでhttp://127.0.0.1:8080/api/にアクセスするとこんな感じでJSONが取得できます。

{"now": "2018-12-26 14:00:29.432784"}

後から分かったこと(readinessProbe、livenessProbeへの対応)

目的はminishift上でDjango2を動かすことですので…
後で気が付きましたがminishift(openshift)でPodのヘルスチェックが仕込まれています。

その内容はURLのルートにアクセスして200_OKを返すかどうか。
よってURLルートにアクセスしてきた場合は空コンテンツを返すよう修正します。

restapi/views.py
from datetime import datetime
from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
from rest_framework import status
from rest_framework.decorators import api_view

# Create your views here.

@api_view(['GET'])
def current_time(request):
    ret = {'now':str(datetime.now())}
    return JsonResponse(ret, status=status.HTTP_200_OK)

def health(request):
    return HttpResponse('')
sample/urls.py
"""sample URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from restapi import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', views.current_time, name='current_time'),
    path('', views.health, name='health'),         # ここを追加
]
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?