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 3 years have passed since last update.

Djangoでwebアプリを作る 基礎設定

Last updated at Posted at 2021-05-24

基本設定/usrls.pyに基本の設定を書く

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

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

1、使うプロジェクトで読み込む関数が変わる
2、

6、include()関数を使ってblog_appのurls.pyファイルを読み込む(アプリようのファイルを読み込む)
5、http://127.0.0.1:8000/adminにアクセスするとadmin.site.urlsを読み込む

アプリ側のurls.pyを作る
同じ名前でややこしい

from django.urls import path
from . import views

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

1,2行目でpathが使えるようにする

path('', views.index, name='index'),ですが、pathの第一引数が空の場合('')この状態→http://127.0.0.1:8000/の時は、views.pyファイルのindex関数を読み込みます。
path(第一引数がからの場合、)
.同じ階層のviewsの中のindexを使えるようにする

同じ階層のviews.pyに書き込む

サイトのリクエスト(どのページhtmlが欲しいか)を入れる
viwesは名前通り

読み込むファイルを作る
1、templateを作る
設定ふぁいるのsettinguのTEMPLATESに
temoletaフォルダーを読み込ませる

import os
をする

import os
from pathlib import Path

になる
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',
],
},
},
]

templateフォルダに
その中にindex.htmlを入れる

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?