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 1 year has passed since last update.

Djangoスタートアップ その4(URL~ビュー作成編)

Last updated at Posted at 2023-02-26

アプリケーションのURLを指定する

※プロジェクト側のurl.pyに変更を加える

(プロジェクト)/urls.py
from django.contrib import admin
from django.urls import path, includeincludeを追記
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('(アプリ名).urls')),
]

http://127.0.0.1:8000
は(アプリ名).urlsへリダイレクトされる

アプリケーション内のURLを指定する

※アプリ側にurl.pyを新規作成

(アプリ名)/urls.py
from django.urls import path
from . import views#viewsをインポート

app_name = (アプリ名)#追加しないと、後に出てくるtemplateからurlを読み込むことができない

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

ビューファイルの作成

  • MVCモデルのControllerに対応する(ちょっとわかりずらい...DjangoのViewはMVCのController
(アプリ名)/views.py
from django.shortcuts import render
def post_list(request):
    return render(request, '<アプリ名>/<出力するHTMLファイル名>.html', {})

views.pyで実装したビューはurls.pypathの第二引数で指定する

参考記事

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?