31
20

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 5 years have passed since last update.

django.urls.reverse についてまとめる

Posted at

django.urls.reverse はよくわからなくなることが多いのでまとめる。

reverse とは

Django の urls に設定された名前をパラメータとして渡すと、URLを返す。

sampleapp.urls.py
# 情報足りてないかも知れませんが、大体で理解してください。

urlpatterns = [
    url(r'^send_mail/$', send_mail_view, name="send_mail"),
    url(r'^item/<pk>/$', item_detail_view, name="item_detail"),
]

上記URL の設定がある前提で、以下のように使う。

reverse("sampleapp:send_mail")
# /send_mail/ を返す

reverse にパラメータを渡す(query)

"".join([reverse('sampleapp:send_mail'),'?',urlencode(dict(limit='10', flg='1'))])
# '/send_mail/?limit=10&flg=1' を返す

reverse にパラメータを渡す(path)

reverse('sampleapp:item_detail', kwargs=dict(pk=u'7'))
# '/item/7/' を返す
31
20
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
31
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?