Django 1.6网址显示意外错误

Django 1.6 urls showing unexpected erros

本问题已经有最佳答案,请猛点这里访问。

我的django app的网址就是这样的形式

url(r'^$', include('app.urls')),

一旦我运行服务器并点击网址,它就会显示错误

invalid syntax

完整的追溯如下:

`Environment:

Request Method: GET

Django Version: 1.6

Python Version: 2.7.3

Installed Applications:

('django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',
'django.contrib.messages',

'django.contrib.staticfiles',

'app')

Installed Middleware:

('django.contrib.sessions.middleware.SessionMiddleware',

'django.middleware.common.CommonMiddleware',

'django.middleware.csrf.CsrfViewMiddleware',

'django.contrib.auth.middleware.AuthenticationMiddleware',

'django.contrib.messages.middleware.MessageMiddleware',

'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:

File"/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response

  • resolver_match = resolver.resolve(request.path_info)
  • File"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve

  • for pattern in self.url_patterns:
  • File"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns

  • patterns = getattr(self.urlconf_module,"urlpatterns", self.urlconf_module)
  • File"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in
    urlconf_module

  • self._urlconf_module = import_module(self.urlconf_name)
  • File"/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module

  • import(name)
  • File"/home/........./urls.py" in

  • url(r'^$', include('.urls')),
  • File"/usr/local/lib/python2.7/dist-packages/django/conf/urls/init.py" in include

  • urlconf_module = import_module(urlconf_module)
  • File"/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module

  • import(name)
  • Exception Type: SyntaxError at /

    Exception Value: invalid syntax (urls.py, line 6)

    完整的网址就是这样的

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    from django.conf.urls import patterns, include, url

    from django.contrib import admin

    admin.autodiscover()

    urlpatterns = patterns('',

        url(r'^', include('mmm.urls')),

       # url(r'^blog/', include('blog.urls')),

        url(r'^admin/', include(admin.site.urls)),
    )

    它真的是一个语法问题吗?
    `


    这条线不正确。

    1
    url(r'^$', include('app.urls')),

    插入符号^匹配字符串的开头,如果字符串,则美元$匹配结尾,因此^$仅匹配索引URL /。 您应该将其更改为:

    1
    url(r'^', include('app.urls')),

    除此之外,如果您不发布整个urls.py,则无法提供帮助。 尽量不要更改代码或错误消息。 例如,当您将其更改为"app.urls"时,您可能会将错误隐藏在代码中。