LoginSignup
1
1

More than 5 years have passed since last update.

pythonフレームワークのdjangoとbottleでHello World!するまでの流れをまとめてみよう

Last updated at Posted at 2019-03-18

少し今まで学んだことの復習をしたいです。 今回はdjangoとbottleでのHello World!までの流れを復習します。

pythonフレームワークのdjangoでHello World!するまでの流れをまとめてみよう ]

djangoはコマンドプロンプトからテンプレートを作成することができます。

コマンドは

py -m django startproject 任意の名前 

これでテンプレートをダウンロードできます。


 tree ディレクトリ名 /f

コマンドでディレクトリ構成を確認。

│  manage.py
│
└─djangotest
        settings.py
        urls.py
        wsgi.py
        __init__.py

これがdjangoの基本的なテンプレートになります。

まずsettings.pyを開きます。

LANGUAGE_CODEはデフォルトは英語なので’ja’に変更しておきましょう。

TIME_ZONEはAsia/Tokyoに! ここまできたら実際にハローワールドをさせるためのアプリケーションを作成しましょう。

ディレクトリのmanage.pyのある部分で

└─djangotest │ manage.py 


py manage.py startapp 任意のアプリ名



これでhelloworldを出力するdjangoアプリケーションのテンプレートが作成されます。



再びディレクトリ構成を確認してみると・・・。

└─djangotest
    │  manage.py
    │
    ├─djangotest
    │  │  settings.py
    │  │  urls.py
    │  │  wsgi.py
    │  │  __init__.py
    │  │
    │  └─__pycache__
    │          settings.cpython-37.pyc
    │          __init__.cpython-37.pyc
    │
    └─helloworld
        │  admin.py
        │  apps.py
        │  models.py
        │  tests.py
        │  views.py
        │  __init__.py
        │
        └─migrations
                __init__.py

helloworld以下が追加されていますね。(今回はアプリ名をhelloworldにしました。)


アプリケーションを作ったらsettings.pyに認識させなくてはいけません。

settings.pyの以下の部分に


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]




'helloworld.apps.HelloworldConfig',



と加えてください。helloworldのところはあなたが作成したアプリケーション名ですよ。

僕はhelloworldとしたのでhelloworldとなっているだけです。

大文字小文字間違えないでね。

次はアプリケーションにアクセスするURLを作ります。

urls.pyという

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]




こんなことが書いてある場所に

from django.urls import path, include←includeを追加

path('アプリをアクセスさせたいURL名', include('作成したアプリケーション名).urls')),



と書いてください。


僕の場合こうなります。

path('helloworld', include('helloworld.urls')),



しかし実はアプリケーションの方にもurls.pyを作らないといけません。

└─djangotest
    │  manage.py
    │
    ├─djangotest
    │  │  settings.py
    │  │  urls.py
    │  │  wsgi.py
    │  │  __init__.py
    │  │
    │  └─__pycache__
    │          settings.cpython-37.pyc
    │          __init__.cpython-37.pyc
    │
    └─helloworld
        │  admin.py
        │  apps.py
        │  models.py
        │  tests.py
        │  views.py
        │  __init__.py
        │
        └─migrations
                __init__.py

helloworldディレクトリの中にurls.pyというファイルを追加しましょう。

urls.pyにはまずこう記載します。


from django.urls import path
from . import views

from django.urls import pathと書かないとurlにパスが通りません。

またfrom . import viewsは後ほどこのurlsの中身をviews.pyに記載するので参照できるようにするためにこのように書いておきます。

さらにこの下に


app_name = 'アプリケーション名'

urlpatterns = [

path('', viewsで作成する関数名.html, name='任意の名前'),



というように記載します。

ではviews.pyにhelloworldするための関数を書いていきましょう。



from django.http import HttpResponse

def 先ほど指定した関数(request):
    return HttpResponse("Hello World!")



そうしたらコマンドプロンプトでpy manage.py のあるディレクトリに移動をして、

py manage.py runserver





こんな感じで出てくるので

You have 14 unapplied migration(s). Your project may not work properly until you
 apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
March 18, 2019 - 16:25:27
Django version 2.0.2, using settings 'djangotest.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.




http://127.0.0.1:8000/



このURLを開きます。

・・・がpage not foundになるはずなので。
urlsで指定した・・・。

from django.urls import path, include←includeを追加

path('アプリをアクセスさせたいURL名', include('作成したアプリケーション名).urls')),



アプリをアクセスさせたいURL名!!http://127.0.0.1:8000/ の後ろに追加しましょう。


このURLにアクセスするとHello World!と表示されますよ。

・・・ちなみに。

app_name = 'アプリケーション名'

urlpatterns = [

path('', viewsで作成する関数名.html, name='任意の名前'),




アプリケーション側に記載したURL。

path('/hogeee/', viewsで作成する関数名.html, name='任意の名前'),


とかにするとURLが


http://127.0.0.1:8000/任意のURL名/hogeee/



とかになるので試してみてね。

pythonフレームワークのbottleでHello World!するまでの流れをまとめてみよう

続いてはbottle編。

bottleはdjangoみたいにコマンドプロンプトでテンプレート作ってくれる機能はあるんですかね?

僕はそれ習ったことないので普通にファイルを作成します。

任意のディレクトリを作成してそこにパイソンファイルを作ってください。

僕はこんな感じにしました。



└─helloworld
        helloworld.py




すくなっ!!!!!!!!!



bottleは最軽量のpythonフレーワークなのです。

helloworld.pyにはこのように書きました。

from bottle import route, run

@route('/helloworld')
def helloworld():
    return 'Hello World!'

run(host='localhost', port=8080, debug=True)





bottleからroute, runをimportします。

from bottle import route, run


@route('/helloworld')



routeは先ほどのurls.pyの機能を果たしています。urlを構築しているのです。

その下に関数

def helloworld():
    return 'Hello World!'



ここがdjangoでいうviews.pyの関数部分です。


一番最後に

run(host='localhost', port=8080, debug=True)




この部分がdjangoでいうmanage.pyの部分ですね。


ローカルサーバーがhelloworld.py内に格納されているので、コマンドプロンプトで

py helloworld.py



とするとサーバーが起動、




とURLに打ち込むとHello Worldコマンドが出力されます。

bottleは1つのファイルでアプリケーションが作れるのでお手軽ですが、その分拡張性はdjangoに劣るので自分の開発したいものに合わせて使い分けるといいですね。

1
1
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
1
1