0
1

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のnamespace

Last updated at Posted at 2019-10-15

url.py内に以下にapp_nameという変数を定義すると、namespaceが定義できる。

urls.py
from django.conf.urls import url
from django.urls import path

# app_name is reserved word of Django. This define a namespace in URL
app_name = "testspace"

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'faq', views.faq, name='faq')
    ]

namespaceを使うと、HTML内のurlから以下のように呼び出すことができる。

other.html
<a href="{% url 'testspace:home' %}">Home</a>

逆に、app_nameを途中から付けた場合、これまで{% url 'faq' %}で呼び出せていたものすべてにtest:を付けないと、以下のようなエラーで表示されなくなる。

NoReverseMatchat /
Reverse for 'faq' not found. 'faq' is not a valid view function or pattern name.
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?