0
1

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.

【Python】Django~HelloWorld(views.py & HTML表示)~

Last updated at Posted at 2019-06-19

まずこれは私のやったことなどをまとめるためのメモ帳になります
引っかかったことや思ったことなどをできるだけまとめようと思います

目次

  • はじめに
  • 環境
  • 前提
  • 1.仮想環境の準備
  • 1-1. 仮想環境作成
  • 1-2. 仮想環境起動
  • 1-3. Djangoインストール
  • 1-4. Django導入確認
  • 2-1. views.pyでHellow,world
  • 2-2. HTMLでHellow,world
  • 2-2-1. プロジェクト作成
  • 2-2-2. HTMLファイル作成
  • 2-2-3. 各ファイル設定
  • 2-2-4. Hello,world
  • 終わり

はじめに

環境

  • Windows10 Pro
  • Anaconda
  • Python3.7
  • Django2.2

前提

Anacondaのインストールは終わっているものとします

1.仮想環の準備

コマンドプロンプトを使用して設定していきます

1-1.仮想環境作成

コマンドプロンプトを開いて下記のコマンドを入力する

cmd
conda create -n 環境名

下記の表示が出ますので、Enterを押してください

> conda create -n 環境名
Collecting package metadata: done
Solving environment: done

## Package Plan ##
 enviroment location C:\Anaconda3\envs\環境名

Proceed ([y]/n)?

下記の表示が出れば成功です

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this enviroment, use
#
#  $conda activate 環境名
#
# To deactivate enviroment, use
#
# $conda deactivate

1-2.仮想環境起動

cmd
activate 環境名

左に(環境名)と表示されれば成功です

> activate 環境名
(環境名) >

1-3.Djangoインストール

起動した仮想環境にDjangoを入れていきます

cmd
conda install django

ライブラリを入れる際に必要なライブラリをインストールする許可を聞かれる場合があります
yを入力してインストールしてください(入れていない場合pythonはここでインストールされます)

done

(環境名) >
cmd
python -m django --version

で念のためバージョンを確認
インストールしたバージョンが出れば成功です

1-4.Django導入確認

cmd
django-admin startproject mysite
  • mysite/
    • manage.py
    • mysite/
      • __ init__.py
      • settings.py
      • urls.py
      • wsgi.py

のような構成のファイルが作られます

cmd
cd mysite
cmd
py manage.py runserver

下記のような画面が出ます

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

6月 18, 2019 - 15:50:53
Django version 2.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

http://127.0.0.1:8000/
の部分がアドレスになります(設定などによりアドレスが変わることがあります)

一度アドレスを踏んで確認してください
このようなロケットのページが出れば導入成功です
django_rocket.JPG

2-1. Views.pyでHellow,world

cmd
py manage.py startapp polls
  • polls/
    • __ init__.py
    • admin.py
    • apps.py
    • migrations/
      • __ init__.py
    • models.py
    • tests.py
    • views.py

の構成のpollsフォルダができます

polls/view.py
from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world")

pollsフォルダの中にurls.pyを新たに作成します

polls/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

次はMysiteフォルダ内にあるursl.pyの中身を書き換えます

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

urlpatterns = [
    path('', include('polls.urls')),
    path('admin/', admin.site.urls),
]
cmd
py manage.py runserver

起動して、示されたアドレス" http://127.0.0.1:8000 " を開きます

下記のような画面が表示されます
django_hello.JPG

これでviews.pyでのHello,worldは終わりです。

2-2. HTMLでHello,world

2-2-1. プロジェクト作成

cmd
django-admin startproject hello
  • hello/
    • manage.py
    • hello/
      • __ init__.py
      • settings.py
      • urls.py
      • wsgi.py

のような構成のファイルが作られます

cmd
cd hello

2-2-2. HTMLファイル作成

templatesフォルダーを作成します
その中にindex.htmlを作成します

index.html
<html>
<head>
</head>
<body>
  <h1>Hello,world</h1>
</body>
</html>
  • hello/
    • manage.py
    • hello/
      • __ init__.py
      • settings.py
      • urls.py
      • wsgi.py
    • templates/
      • index.html

上記のような構成になります

2-2-3. 各ファイル設定

settings.pyは下記箇所のみ

settings.py
TEMPLATES = [
{
     'BACKEND': 'django.template.backends.django.DjangoTemplates',
     'DIRS': [
      os.path.join(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',
           ],
       },
    },
]
urls.py
from django.contrib import admin
from django.urls import path
from . import views #追加箇所

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.hello,name='hello'), #追加箇所
]

views.pyを作成して
中身は下記を記述

views.py
from django.http.response import HttpResponse
from django.shortcuts import render

def hello(request):
    return render(request, 'hello.html')

2-2-4. Hello,world

cmd
py manage.py runserver

成功していればこのような画面が表示されます
html_hello.JPG

終わり

途中まで書いて一度放置していたのでおかしいところがあるかもしれませんが何卒ご了承ください
以上djangoのviews.pyとHTMLでのHello,word出力でした
ありがとうございました

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?