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アプリの記述方法

Posted at

なぜアプリが必要か?

様々なページが増えるとテンプレートやデータが増える。
アプリごとに管理するほうがわかりやすい。開発がしやすい。

プロジェクトは一つ。アプリは複数作られる。

まず、プロジェクトのurls.pyがリクエストを受けて、その後にアプリごとの
urls.pyに振り分けられる。

アプリの記述

pycharmでappを作成する。
スクリーンショット 2021-03-22 11.27.38.png

startappコマンド

manage.py@djangoProject > startapp helloworldapp

プロジェクトのurls.pyからアプリのurls.pyにつなげるためにincludeを使用する。

djangoProject/urls.py
from django.contrib import admin
from django.urls import path, include
from .views import helloworldfunction, HelloWorldView

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

アプリのurls.pyでは、hello/を抜いていることに注意。

helloworldapp/urls.py
from django.urls import path
from .views import hellofunction

urlpatterns = [
    path('world/', hellofunction),
]
helloworldapp/views.py
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def hellofunction(response):
    return HttpResponse('hello')
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?