[ERROR:] 'polls' is not a registered namespace

Hello,

I am learning Django using the official django CMS 3.8.x tutorials.

So, in section 3.1.1, when I added re_path(r’^polls/’, include(‘polls.urls’)) to the urlpatterns tuple in urls.py, I ran into some error. By changing it to path(“polls/”, include(“polls.urls”)), it worked fine. So I continued using the latter.

Now, in section 5.1.2., where it says to remove the old polls entry from the project’s urls.py, if I remove the path(“polls/”, include(“polls.urls”)), I am getting the error:
NoReverseMatch at /en/
‘polls’ is not a registered namespace

A screenshot of the error is attached, if that helps in troubleshooting.

Now, please help me understand:

  1. whether going ahead with path(“polls/”, include(“polls.urls”)) instead of re_path(r’^polls/’, include(‘polls.urls’)) was the correct choice?
  2. whether the section 5.1.2 is actually asking me to remove the above line of code, or some other line of code? I have this doubt because the line suggested to be removed in section 5.1.2. has an additional namespace parameter, which isn’t present in either path(“polls/”, include(“polls.urls”)) or re_path(r’^polls/’, include(‘polls.urls’)).
  3. If the line suggested to be removed in section 5.1.2. is indeed some other line of code, please help me with correcting the wrongs I have committed to be able to continue with the tutorials.

Thanks.

We’d have to see exactly what you had in the original re_path case to verify that. It’s possible you had a typo in your re_path definition that caused an error and that you fixed the error in the process of changing it from re_path to path.

If you really want to learn from it, you could change it back to the re_path line to see if you get the same error - and if so, post the contents of your urls.py file here for assistance.

For this step to work, the previous step (5.1.1) had to have been completed successfully. So the first thing we need to verify is the contents and location (directory) of your cms_apps.py file.

(Note: When posting code here, enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.)

1 Like

Hi Ken,

Thanks for the response.

  1. There was no re_path in my urls.py. The urlpatterns in my urls.py were as follows:
urlpatterns += i18n_patterns(
    path("admin/", admin.site.urls),
    path("", include("cms.urls"))
)

After section 3.1.1., the urlpatterns in my ‘urls.py’ looked like this:

urlpatterns += i18n_patterns(
    path("admin/", admin.site.urls),
    path("polls/", include("polls.urls")),
    path("", include("cms.urls"))
)
  1. Whenever I try using re_path instead of path, I get the NameError: name 're_path' is not defined as follows:
(venv) D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\mysite>python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "c:\program files\python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "c:\program files\python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\urls\resolvers.py", line 412, in check
    for pattern in self.url_patterns:
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\urls\resolvers.py", line 593, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\venv\lib\site-packages\django\urls\resolvers.py", line 586, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\program files\python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\mysite\mysite\urls.py", line 19, in <module>
    re_path("", include("cms.urls"))
NameError: name 're_path' is not defined
  1. Here’s the directory structure to verify the location of cms_apps.py:

  2. The contents of cms_apps.py are as follows:

from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool


@apphook_pool.register  # register the application
class PollsApphook(CMSApp):
    app_name = "polls"
    name = "Polls Application"

    def get_urls(self, page=None, language=None, **kwargs):
        return ["polls.urls"]

I’m not specifically familiar with Django CMS or this tutorial, so I don’t know where this may be covered - if it is - but that error message is a result of the function not being imported into the module. Adding the line:
from django.urls import re_path
near the top of your urls.py file with your other import statements will resolve that issue. (See the docs for re_path)

I don’t see any issue with your cms_apps.py file.

In that same section (5.1.2), it says:

Not only is it not required there, because we reach the polls via the apphook instead, but if you leave it there, it will conflict with the apphook’s URL handling. You’ll receive a warning in the logs:

(and then goes on to show the error)

If you leave that entry in your urls.py file, do you get the described error?

If not, then there’s something else missing or wrong.

Going back to section 3 of the docs, I see:

For this tutorial, we’re going to take a basic Django opinion poll application and integrate it into the CMS.

I’m assuming you already have a functioning polls application installed as an external app? (I don’t see it in your directory structure within your project.)

1 Like

Right. I see. Thanks!

Yes. I do get that warning.

(venv) D:\Tutorials\Python\Projects\djangoCMS-tutorial-project\mysite>python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified some issues:

WARNINGS:
?: (urls.W005) URL namespace 'polls' isn't unique. You may not be able to reverse all URLs in this namespace

System check identified 1 issue (0 silenced).
May 20, 2022 - 18:30:39
Django version 3.1.14, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CTRL-BREAK.

But I guess since it’s only a warning instead of being an error, django continues to render the website… not sure… :man_shrugging:

Yes. I was also confused at first as to why it wasn’t showing up in PyCharm’s project directory. But I saw one polls directory (project?) in .\djangoCMS-tutorial-project\venv\Lib\site-packages\. So I persisted with it through sections 3 & 4 and was able to complete them without any warnings or errors.

(post deleted by author)

That’s true - but not the reason I was asking about it.

Since you’re getting that message, my assumption would be that both the cms_apps.py file and your urls.py file are configured correctly according to Django CMS. I would then have guessed that since that is the case, removing the polls entry from urlpatterns woud not create an error.

Changing directions slightly - what URL are you entering to get to that page where this error appears? Your address bar shows localhost:8000/en/, but I’m not seeing any step where you are supposed to enter that as a URL - so I’m guessing that you’re going to a different URL, and it’s sending you there.

Yeah. So I read somewhere, I think in the djangoCMS docs itself (I’m honestly unable to locate the text at this point :smiling_face_with_tear:), that I could pass port and/or IP as a parameter to python manage.py runserver. And leveraging that, if I pass 0:8000, then I’d be able to accept incoming connections from other computers in the network.
Reading that, I was like “Cool! Let me try that and see if I could access this from my laptop, besides my desktop.” And when I accessed it from the laptop, it threw me some error that I’d need to add the IP to ALLOWED_HOSTS in my settings.py. I did that and it worked. So at that point, my ALLOWED_HOSTS in my settings.py looked like this:

ALLOWED_HOSTS = [
    '172.18.138.134'
]

But then back on my desktop, I wasn’t able to access the website using http://localhost:8000/en/ and I guess Django hinted me to add localhost to ALLOWED_HOSTS too and I did that and it worked. So my ALLOWED_HOSTS in my settings.py looks like this now:

ALLOWED_HOSTS = [
    '172.18.138.134',
    'localhost'
]

So while on terminal, I do python manage.py runserver 0.0.0.0:8000, fetching the command from terminal’s memory, in my browser, I simply either refresh or copy-paste the http://localhost:8000/en/ url. It’s just that I am just too lazy to type things from scratch in my terminal or in my browser! :laughing:

Sorry, my question wasn’t clear. I was only asking about the /en/ portion of the url. I understand about the rest. I didn’t notice any reference to a /en/ url, that’s all.

That, I have no idea why its there. :sweat_smile:
The installation process I followed, I wrote it here. Maybe that holds some clue to that /en/ url…