坚持django的URL模式

时间:2012-03-10 01:44:51

标签: regex python-2.7 django-urls django-1.3

您好我在django中使用以下网址模式,我想知道问题出在哪里以及为什么无法找到它。以下屏幕截图显示了我的URL以及预期的URL。 enter image description here

请告诉我哪里弄错了。请告诉我是否可以告诉其他事情来理解问题。我在它上面看了差不多一个小时或者两个小时然后我把它发布在stackoverflow.com上,所以希望我能得到一些解决方案。

在这里添加urls.py:

from django.conf.urls.defaults import patterns, include, url 
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'ecomstore.views.home', name='home'),
#url(r'^catalog/', 'preview.views.home'),

# url(r'^ecomstore/', include('ecomstore.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
 url(r'^admin/', include(admin.site.urls)),
 url(r'',include('catalog.urls')),
  )

然后是目录app的urls.py:

from django.conf.urls.defaults import *
urlpatterns=patterns('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'),

先谢谢你们。

1 个答案:

答案 0 :(得分:0)

参考我的评论:至少有一件事是你在&#34; product&#34;之前有一个额外的斜线。你想要^产品,而不是^ /产品。这可能就是全部,但从图片中说起来有点难以理解。

另外,如果我错了,请纠正我,因为我暂时没有使用正则表达式,但不会[ - _ \ w +]只匹配 - 或_或\ w +?< / p>

&#34; []

将方括号内的任何内容与一个字符位置匹配一次且仅一次,例如,[12]表示将目标与1匹配,如果不匹配则将目标与2匹配,而[0123456789]表示与任何匹配0到9范围内的字符。&#34;

http://www.zytrax.com/tech/web/regex.htm

所以你想要...... [-_] \ w +?