Django ブログサイト urls.py アプリURL

2020/07/04 (更新:2020/11/19)

アプリのURL定義です。

ビュー名ルート指定例
default_view
redirect_viewredirect_viewredirect_view
index<author>/sample/
detail<author>/<blog>/sample/5
search<author>/searchsample/search
comment<author>/comment/<blog>/sample/comment/5/
comment_update<author>/comment/<comment>/updatesample/comment/5/update
reply<author>/reply/<comment>/sample/reply/6/

default_viewはルート指定のビューで、デフォルト投稿者のブログ初期画面indexにリダイレクトします。
detail_testはテンプレート確認用の画面です。

blog/urls.py

app_name = 'blog'
urlpatterns = [
    path('', views.default_view, name='default_view'),
    path('redirect', views.redirect_view, name='redirect_view'),
    path('<slug:author_name>/', views.BlogIndex.as_view(), name='index'),
    path('<slug:author_name>/<int:pk>/', views.BlogDetail.as_view(), name='detail'),
    path('<slug:author_name>/search', views.BlogSearch.as_view(), name='search'),

    path('<slug:author_name>/comment/<int:post_id>/', views.comment, name='comment'),
    path('<slug:author_name>/comment/<int:comment_id>/update', views.comment_update, name='comment_update'),
    path('<slug:author_name>/reply/<int:comment_id>/', views.reply, name='reply'),
]