urls.py中的例外情况

时间:2012-09-13 06:30:14

标签: python regex django url

我花了很多时间来追踪我的错误但我找不到任何人可以找出错误的位置吗?

Environment:


Request Method: GET

Request URL: [http://127.0.0.1:8000/blog/archives]

Django Version: 1.4.1

Python Version: 2.7.3

Installed Applications:

('django.contrib.auth',

 'django.contrib.contenttypes',

 'django.contrib.sessions',

 'django.contrib.sites',

 'django.contrib.messages',

 'django.contrib.staticfiles',

 'django.contrib.admin',

 'taggit',

 'blog')

Installed Middleware:

('django.middleware.common.CommonMiddleware',

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

 'django.middleware.csrf.CsrfViewMiddleware',

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

 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  89.                     response = middleware_method(request)

File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in process_request
  67.             if (not urlresolvers.is_valid_path(request.path_info, urlconf) and

File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in is_valid_path
  531.         resolve(path, urlconf)

File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  420.     return get_resolver(urlconf).resolve(path)

File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  300.                     sub_match = pattern.resolve(new_path)

File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  300.                     sub_match = pattern.resolve(new_path)

File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  196.         match = self.regex.search(path)

File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in regex
  162.                 compiled_regex = re.compile(self._regex, re.UNICODE)

File "/usr/lib/python2.7/re.py" in compile
  190.     return _compile(pattern, flags)

File "/usr/lib/python2.7/re.py" in _compile
  242.         raise error, v # invalid expression

Exception Type: error at /blog/archives
Exception Value: unexpected end of pattern

这里是我的URLS.PY

from django.conf.urls import patterns, include, url

from django.views.generic import ListView, DetailView

from blog.models import Post

urlpatterns = patterns('',

    url(r'^$', 
        ListView.as_view(
               queryset=Post.objects.order_by("-created")[:2],
               template_name="blog.html")),

      url(r'^(?p<pk>\d+)/$',
        DetailView.as_view(
                model=Post,
                template_name="post.html")), 
    url(r'^archives/$', 
        ListView.as_view(
                queryset=Post.objects.all().order_by("-created"),
                template_name="archives.html")), 
    url(r'^tags/(?p<tag?\w+)/$', 'tagpage'),                        

)

3 个答案:

答案 0 :(得分:6)

$url(r'^tags/(?p<tag?\w+)/$', 'tagpage'),

&gt;

替换字符

答案 1 :(得分:4)

可能在url(r'^tags/(?p<tag?\w+)/$', 'tagpage')中出现错误? 我认为它应该是:url(r'^tags/(?p<tag>\w+)/$', 'tagpage')

答案 2 :(得分:1)

?代替>外,p也需要大写。

r'^tags/(?P<tag>\w+)/$'