Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#34189 closed Bug (invalid)

Code in Tutorial does not work because of a ','

Reported by: Roger Rüttimann Owned by: nobody
Component: Documentation Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Page: https://docs.djangoproject.com/en/4.1/intro/tutorial01/

Code

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

the ',' at the end of path(...) needs to be removed, otherwise an exception is raised and the code does not work.

it should be:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index')
]

I am sorry in case I violite some buge tracking conventions. I actually want to continue with the tutorial, but wanted to raise the issue. Thanks for the otherwise greate tutorial! :)

Change History (4)

comment:1 by Mariusz Felisiak, 2 years ago

Resolution: invalid
Status: newclosed

urlpatterns is a list and this example is a valid Python code. Trac is not a support channel, so if you're having trouble understanding how Python/Django works, see TicketClosingReasons/UseSupportChannels for ways to get help.

comment:2 by Roger Rüttimann, 2 years ago

Thanks for the comment @Mariusz Felisiak, but even as the code might be valid python, I still think its a bug, because this happens when I copy paste the code of the tutorial and try to run the development server:

(cvmanager) ➜  cvmanager git:(main) ✗ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/urls/resolvers.py", line 717, in url_patterns
    iter(patterns)
TypeError: 'module' object is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/core/management/base.py", line 475, in check
    all_issues = checks.run_checks(
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    return check_method()
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/urls/resolvers.py", line 495, in check
    messages.extend(check_resolver(pattern))
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    return check_method()
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/urls/resolvers.py", line 494, in check
    for pattern in self.url_patterns:
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/rroger/py_virtualenvs/cvmanager/lib/python3.10/site-packages/django/urls/resolvers.py", line 725, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'cv.urls' from '/Users/rroger/workspace/cvmanager/cv/urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

and without the ',' it works as expected. I think code in a tutorial should run without raisng exceptions.

comment:3 by Mariusz Felisiak, 2 years ago

I still think its a bug, because this happens when I copy paste the code of the tutorial and try to run the development server:

Maybe there is a bug but not in the tutorial or Django itself, but in your code. You probably made some typo when copying code from the tutorial. Please use one of support channels.

comment:4 by Roger Rüttimann, 2 years ago

Actually your right, I tried to reprodcue it, but I can not. Now it's working either way, with or without the comma.
Sorry for the unproductive ticket and thanks for the quick feedback!

Note: See TracTickets for help on using tickets.
Back to Top