我收到以下错误:view_page() takes exactly 2 arguments (1 given)
view.py的代码是:
from wiki.models import Page
from django.shortcuts import render_to_response
def view_page(request, page_name):
try:
page = Page.objects.get(pk=page_name)
except PageDoesNotExist:
return render_to_response("create.html", {"page_name":page_name})
url.py中提供的网址是:
url(r'^wiki/$', 'wiki.views.view_page'),
但是当我按下面给出的url并删除上面的那个时,我收到了找不到页面的错误。
url(r'^wiki/(?P<page_name>[^/]+)/$','wiki.views.view_page'),
答案 0 :(得分:1)
这是因为view_page()的第二个参数在url
中传递例如
url(r'^wiki/(?P<page_name>[^/]+)/$','wiki.views.view_page')
something.com/wiki/2 #here 2 is the second parameter which is like
def view_page(request, 2)
如果你使用这个
url(r'^wiki/$', 'wiki.views.view_page'),
something.com/wiki/ #The second parameter is not passed to the function
答案 1 :(得分:0)
我认为您的查询集错了。如果您在URL中传递page_name,那么它应该如下所示:
Page.objects.get(name=page_name)
答案 2 :(得分:0)
也许第二个参数应该是可选的?
def view_page(request, page_name="Default value"):