プロジェクトにviewを作成 #django

プロジェクトにviewを作成

プロジェクトのURL

testprj/urls.py

from django.contrib import admin
from django.urls import include, path
from django.conf.urls.static import static
from django.conf import settings

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

アプリケーションのURL

testprj/testapp/urls.py

from django.conf.urls import url
from django.urls import include, path
from . import views

urlpatterns = [
    #develop_log : 正規表現はダメらしい
    #develop_log : module 'testprj.testapp.views' has no attribute 'index'
    #path('', views.index, name='index'),
    path('', include('testprj.testapp.views.urls')),
    #develop_log : 下記はなくてもいい。下のディレクトリで定義できる
    path('app1/', include('testprj.testapp.views.urls')),
]

viewのviewのURL

testprj/testapp/views/urls.py

from django.conf.urls import url
from django.urls import include, path
from . import views, views2

urlpatterns = [
    #develop_log : 正規表現はダメらしい
    path('', views.index, name='index'),
    #develop_log : pathの最後には'/'が必要
    path('app2/', views2.index, name='index'),
]

END

Close