0

I just upgraded my Django to version 1.9.6. I was doing the basic tutorial but I have a Syntax Error in the urls.py.

Note: I changed the name "mysite" to "polls", and "polls" to "poll".

This is my code in polls/poll/urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^, views.index, name='index'),
]

And this is the error message:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03AE16F0>
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 116, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "C:\Python34\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python34\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Python34\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python34\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\David\Dropbox\Emprendimiento\Gamification\polls\polls\urls.py", line 20, in <module>
    url(r'^poll/', include('poll.urls')),
  File "C:\Python34\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1467, in exec_module
  File "<frozen importlib._bootstrap>", line 1572, in get_code
  File "<frozen importlib._bootstrap>", line 1532, in source_to_code
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\David\Dropbox\Emprendimiento\Gamification\polls\poll\urls.py", line 6
    url(r'^, views.index, name='index'),
SyntaxError: invalid syntax

Thanks for the help

1 Answer 1

1

You have missed out the ' to end the string (before the comma in r'^,. Ideally, you should add a $ as well:

url(r'^$', views.index, name='index'),

Note that your text editor or IDE may do syntax highlighting to help you spot this question - note that in your question, Stack Overflow has all of ^, views.index, name= in the same colour, to show that it's a single string.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much @Alasdair. But now I'm getting this message: """" Page not found (404) Request Method: GET Request URL: 127.0.0.1:8000 Using the URLconf defined in polls.urls, Django tried these URL patterns, in this order: ^poll/ ^admin/ The current URL, , didn't match any of these. You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. """ Your answer makes sense, but I only copied and pasted.
That 404 is expected. You haven't defined a url for / yet, you defined it for /poll/ (because of the include). The tutorial tells you to go to http://localhost:8000/polls/, not http://localhost:8000. In your case, go to http://localhost:8000/poll/, or change the include to ^polls/'.
Of course!, thank you very much @Alasdair. You saved me hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.