15
8

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.

Memo:[Django]バージョンによるurls.py内のincludeの仕様の違い

Posted at

Django Tutorial Memo

概要

Urlディスパッチャの記述でハマった。

問題

旧DjangoのTutorialでは、下記のようにしなければならなかった。

old_version

urlpatterns = [
    url(r'^testapp/', include('testapp.urls', namespace='testapp')),
]

新しいDjangoで上記を写経すると下記のエラーがでる。(Django==2.0.3を使用)

Error
    url(r'^testapp/', include('testapp.urls', namespace='testapp')),
  File "C:\Users\****\PycharmProjects\Tutorial\venv\lib\site-packages\django\urls\conf.py", line 39, in include
    'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.ImproperlyConfigured: 
Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

'Specifying a namespace in include() without providing an app_name '
Googleさん:'app_nameを指定せずにinclude()で名前空間を指定する'

解決策

新Django(Django==2.0.3)では、下記のようにすることでエラーをはかなくなった。
要するに、namespaceでapp_nameを指定するのではなく、2-タプルで渡してね。ってことみたい。

new_version
urlpatterns = [
    url(r'^testapp/', include(('testapp.urls', 'testapp'),)),
]

15
8
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
15
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?