Django教程中的错误:urls.py

时间:2016-05-04 21:37:56

标签: django url

我刚刚将我的Django升级到版本1.9.6。我正在做basic tutorial,但我在urls.py中有一个语法错误。

注意:我将名称“mysite”更改为“polls”,并将“polls”更改为“poll”。

这是我在polls / poll / urls.py中的代码:

from django.conf.urls import url

from . import views

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

这是错误消息:

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

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您错过了'来结束字符串(在r'^,中的逗号之前。理想情况下,您还应添加$

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

请注意,您的文本编辑器或IDE可能会进行语法突出显示以帮助您发现此问题 - 请注意,在您的问题中,Stack Overflow的所有^, views.index, name=都采用相同的颜色,以表明它是&#39; sa单串。