ImproperlyConfiguredError about app_name when using namespace in include()

前端 未结 5 1850
北荒
北荒 2020-12-02 14:59

I am currently trying out Django. I use the namespace argument in one of my include()s in urls.py. When I run the server and try to browse, I get t

5条回答
  •  囚心锁ツ
    2020-12-02 15:28

    I am also face the same error in Django 2.2 and i solve it this way

    urls.py file

    urlpatterns = [
       path('publisher-polls/', include('polls.urls', namespace='publisher-polls')),
    ]
    

    polls/urls.py file

    app_name = 'polls'
    urlpatterns = [
      path('', views.IndexView.as_view(), name='index')
    ]
    

    example use of namespace in calss based view method

    def get_absolute_url(self):
        from django.urls import reverse
        return reverse('polls.index', args=[str(self.id)])
    

    example use of namespace in templates

    {% url 'polls:index' %}
    

    Here polls:index mean app_name[define in polls/urls.py file]:name[define in polls/urls.py file inside path function]

    their official which is pretty good you can check for more info namespace_django_official_doc

提交回复
热议问题