目次
URLのテンプレート表示
<a href="{% url 'index' %}">index</a>
<a href="{% url 'admin:index' %}">admin</a>
urlで設定したnameで呼び出す
URLの設定
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
template
{% url 'detail' 34 %}
View
detail(request: HttpRequest, question_id: int):
print(question_id)
34
templateViewの場合
def get(self, request: HttpRequest, *args, **kwargs):
print(kwargs['question_id'])
34
【Django】リクエストからURLを取得する方法