在Django中不是有效的正则表达式错误

时间:2013-05-08 14:59:25

标签: django

请参阅下面给出的urlpatterns,当我尝试运行程序时出现以下错误...

ImproperlyConfigured at /
"^product/(?p<product_slug>  [-\w]+)/$" is not a valid regular expression: unexpected end of pattern


Request Method:
GET 

Request URL:
http://127.0.0.1:8000/ 

Django Version:
1.5.1 

Exception Type:
ImproperlyConfigured 

Exception Value:
"^product/(?p<product_slug>  [-\w]+)/$" is not a valid regular expression: unexpected end of pattern


Exception Location:
C:\Python27\lib\site-packages\django-1.5.1-py2.7.egg\django\core\urlresolvers.py in regex, line 178 

Python Executable:
C:\Python27\python.exe 

urls.py

urlpatterns = patterns('ecomstore.catalog.views',
            (r'^$','index',{'template_name':'catalog/index.html'},'catalog_home'),
            (r'^category/(?P<category_slug>[-\w]+)/$','show_category',{'template_name':'catalog/category.html'},'catalog_category'),
            (r'^product/(?p<product_slug>  [-\w]+)/$','show_product',{'template_name':'catalog/product.html'},'catalog_product'),
        )

views.py

def index(request,template_name="catalog/index.html"):
    page_title = 'Music instruments and Sheet music for musicians'
    return render_to_response(template_name,locals(),context_instance=RequestContext(request))

def show_category(request,category_slug,template_name="catalog/category.html"):
    print 'In Catalog views|category_slug=', category_slug
    c = get_object_or_404(Category,slug=category_slug)
    products = c.product_set.all()
    page_title = c.name
    meta_keywords = c.meta_keywords
    meta_description = c.meta_description
    return render_to_response(template_name,locals(),context_instance=RequestContext(request))

def show_product(request,product_slug,template_name="catalog/product.html"):
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.filter(is_active=True)
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    return render_to_response(template_name,locals(),context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:5)

您只需将?p大写,所以:

r'^product/(?P<product_slug>  [-\w]+)/$'