2016-01-27 30 views
1

试图创建一个简单的论坛。我将如何在Django 1.9中创建一组命名的URL?该文件说做这样的:Django中的多个参数网址

url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive), 

,我做到了,如:

url(u'^(?P<Name>.*)/(?P<ThreadName>.*)/$', views.thread, name = "thread"), 

和views.py:

def thread(request, Name, ThreadName): 
board = get_object_or_404(Board, Title = Name) 
thread = get_object_or_404(Thread, Title = ThreadName) 
context = { "board": board, 
      "thread": thread, 
      } 
return render(request, "post/board.html", context) 

,但我得到一个错误说“没有董事会匹配给定的查询。“,即使董事会存在,我可以到达它的网站/名称,它只会在网站/名称/线程名称失败。

回答

3

您需要使捕获部分非贪心。替换:

url(u'^(?P<Name>.*)/(?P<ThreadName>.*)/$', views.thread, name = "thread"), 

有:

url(u'^(?P<Name>.*?)/(?P<ThreadName>.*?)/$', views.thread, name = "thread"), 

?人物 - 他们使所有的差异。


,或者取决于什么的名称和主题名称的有效字符,你可以找一个或多个alhanumeric\w+),而不是任意字符:

url(r'^(?P<Name>\w+)/(?P<ThreadName>\w+)/$', views.thread, name = "thread"), 
+0

尝试,但我仍然会得到相同的错误。 –