LoginSignup
13
5

More than 3 years have passed since last update.

DjangoでHello Worldアプリの作成手順

Last updated at Posted at 2019-05-13

概要

Djangoで指定されたURLにアクセスすると、Hello Worldと表示させるような、簡単なアプリケーションを作成します。

なお、環境はpython 3.7.3django 2.2になります。

1. プロジェクト作成

1-1. まずはプロジェクトファイルを作りたいので、下記のコマンドでディレクトリを作成します。

terminal
mkdir helloworld

    
1-2. 次にhelloworldprojectというプロジェクトを作成します。最後の.は正しいので、そのとおり実行してください。(ディレクトリはhelloworldであることを確認してください)

terminal
~/helloworld$ django-admin startproject helloworldproject .

  
1-3. コマンドを実行するとhelloworldディレクトリには、下記のようにファイルが生成されます。

helloworld/
helloworld
├── helloworldproject
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

2. アプリケーションを作成

2-1. 新しくアプリケーションを作成したいので、下記のコマンドを実行します。今回は、helloworldappというアプリケーションです。

terminal
~/helloworld$ python manage.py startapp helloworldapp

実行したらディレクトリは下記のようになると思います。

ざっくり3つに分けると、helloworldプロジェクトファイル内に、①helloworldappというアプリケーション、②helloworldprojectというプロジェクト、③manage.pyファイルになります。

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

  
2-2. helloworldprojectsettings.pyにあるINSTALLED_APPSに、hellowroldappを追加します。

helloworldproject/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #追加
    'helloworldapp',
]

  
2-3. Hello Worldを書く、index.htmlを表示できるようTEMPLATESにも設定をします。DIRS[BASE_DIR, 'templates'を追加します。

helloworldproject/settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        #追加
        'DIRS': [BASE_DIR, 'templates'],
        #ここまで
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

3. Hello Worldを表示させよう

3-1. まずは、helloworldproject/urls.pyに下記のように追加してください。

helloworldproject/urls.py
from django.contrib import admin
#includeを追加
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    #追加
    path('', include('helloworldapp.urls'))
]

  
3-2. helloworldappurls.pyファイルを新規作成して、下記のコードを追加してください。これで、https://サイト名.com/helloとアクセスすれば、helloworldappが実行されるようになります。

hellowordapp/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.helloworldfunction),
]

  
3-3. 後ほど作成するindex.htmlファイルにアクセスできるよう、views.pyにコードを追加してください。

hellowordapp/views.py
from django.shortcuts import render

#追加
def helloworldfunction(request):
    return render(request, 'index.html')

  
3-4. templatesフォルダーをhelloworldに新規作成してください。その中にindex.htmlを新規作成し、下記のようにしてください。

helloworld/templates/index.html
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello World</h1>
  </body>
</html>

  
ディレクトリは下記のようになるはずです。

helloworld/
helloworld
├── helloworldapp
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── helloworldproject
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   └── settings.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
└── templates
    └── index.html

  
3-5. 最後に下記のコマンドを実行してください。

terminal
~/helloworld$ python manage.py migrate

4. localhost起動

4-1. localhostを起動して、Hello Worldが表示されるか確認したいので、runserverを実行します。

terminal
~/helloworld$ python manage.py runserver

  
4-2. http://localhost:8000/helloをブラウザーで確認して、Hello Worldと表示されていれば、正しく実行できています。

以上
 
関連:DjangoでHello Worldアプリを、Class Based Viewで表示せてみた

13
5
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
13
5