LoginSignup
1
2

More than 1 year has passed since last update.

Djangoを触ってみる

Last updated at Posted at 2021-04-29

pythonの基礎を身に着けたので何かフレームワークを触りたいなと思い、Djangoを触ってみることにしました。

環境構築

pythonを入れる

こちらのチュートリアルの通りに進める
https://docs.djangoproject.com/ja/3.1/intro/tutorial01/

こんな感じでコマンドを叩くとプロジェクトが作成できる

django-admin startproject myapp

こんな感じでコマンドを叩くとサーバーが起動できる

python manage.py runserver

http://127.0.0.1:8000/
にアクセスしてスタートページを起動

アプリの作成

下記コマンドでポーリングアプリなるものが作成できる。pollsディレクトリが作成される。

python manage.py startapp polls

とりあえずビューを作る

polls/views.py
from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")
polls/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
myapp/urls.py
from django.contrib import admin
from django.urls import include, path

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

http://127.0.0.1:8000/polls/
にアクセスしてページが表示できることを確認する。

Database の設定

settings.pyを開く。

データベースの設定について、今回は試すだけなのでデフォルトのSQLiteを使用する。
TIME_ZONEを設定するらしいので
TIME_ZONE = 'UTC'→TIME_ZONE = 'Asia/Tokyo'に変更する。

以下のコマンドを実行するとテーブルが作成されるらしい

python manage.py migrate

githubにアップロードしてみる

プロジェクトを作ったディレクトリで下記コマンドを実行する。

git init
git add -A
git commit -m "new"
git push origin 作ったリポジトリ名

↑エラーがでたため、下のコマンドを実行

git remote add origin 作ったリポジトリ名
git push origin master

今回はここまで、続きはまた今度

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