Django Error u“'polls” is not a registered namespace

后端 未结 13 2599
误落风尘
误落风尘 2021-01-31 07:41

Yesterday I was working on my first app using this tutorial. It\'s a Poll and Choice app. The first page displays the question and when you click on the question it\'s suppose t

相关标签:
13条回答
  • 2021-01-31 08:04

    The answer is to add namespaces to your root URLconf. In the mysite/urls.py file (the project’s urls.py, not the application’s), go ahead and change it to include namespacing:

    urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
    )
    

    Furthermore, in part 3 of the tutorial Namespacing URL names the use of app_name is mentioned as the accepted way for adding the polls namespace. You can add the line for this in your polls/urls.py as follows:

    app_name = 'polls'
    urlpatterns = [
        ...
    ]
    
    0 讨论(0)
  • 2021-01-31 08:06

    The problem is in the tutorial. While adding the namespace( in your case 'myapp') to your URLconf, the tutorial uses the following code of line:

    app_name = 'myapp'
    

    The Django framework for some reason treats it as a unicode string. Instead please enclose the name of your app in double quotes instead of single quotes. For example,

    app_name = "myapp"
    

    This will most certainly solve your problem. I had the same problem and doing so solved it.

    0 讨论(0)
  • 2021-01-31 08:12

    You need to add the following line to the top of the detail.html:

    {% load url from future %}
    

    (Notice you've already used this line in the index.html in order to use the polls namespace)

    0 讨论(0)
  • 2021-01-31 08:16

    Inside myapp/urls.py add the following module-level attribute:

    app_name = "polls"
    

    This will set the "application namespace name" for that application. When you use names like "polls:submit" in a reverse, Django will look in two places: application namespaces (set like above), and instance namespaces (set using the namespace= parameter in the "url" function). The latter is important if you have multiple instances of an app for your project, but generally it's the former you want.

    I had this very issue, and setting namespace= into the url() function seemed wrong somehow.

    See this entry of the tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial03/#namespacing-url-names

    Update: this information is correct for Django 1.9. Prior to 1.9, adding a namespace= attribute to the include is, indeed, the proper way.

    0 讨论(0)
  • 2021-01-31 08:16

    Restart the web server. Just that.

    0 讨论(0)
  • 2021-01-31 08:20

    Replacing the line: {% url 'polls:vote' poll.id %} with: {% url 'vote' poll.id %} worked out for me...

    0 讨论(0)
提交回复
热议问题