0

我是 django 的新手。当 url 类似于http://127.0.0.1:8000/details/music and concerts

为此,我尝试在我的

网址.py

from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
    url(r'^details/(?P<event_cat>\w{0,50})/$', 'onthemove.views.category'),
)

我的模板 index.html 是

 <div class="cat">
{% if latestevents.count > 0 %}
{% for category in latestevents %}
            <a href="/details/{{ category.cat }}/">
                <div id="per_cat">
                <img src="{{STATIC_URL}}/static/images/music.jpg" />
<span>{{category.cat}}</span>


            </div></a>
{% endfor %}
{% else %}
<span>none to show</span>
{% endif %}

其中 latestevents 从 views.py 传递为:

def searchget(request):
latestevents = events.objects.all().order_by('id')[:1]
return render_to_response('views/search.html',{'latestevents':latestevents})

但这显示错误:

The current URL, details/Music and Concerts/, didn't match any of these.

他们有什么错误吗?

4

2 回答 2

1

\w仅匹配字母数字字符,因此不匹配空格。也许您可以使用r'^details/(?P<event_cat>.{0,50})/$',但这仍然有点奇怪:为什么将其限制为 50 个字符?

于 2013-10-15T13:34:44.630 回答
1

您不希望在您的 URL 中有空格。相反,尝试一种更易于阅读的格式,例如“word-word”,这将是这种模式:

url(r'^details/(?P<event_cat>[-\w]+)/$', 'onthemove.views.category'),

这将匹配:/details/music-and-concerts/

于 2013-10-15T13:35:00.190 回答