TypeError:render_page()只需2个参数(给定1个)

时间:2012-08-08 20:59:44

标签: python django typeerror

在尝试访问我正在尝试构建的个人网站时,我一直收到以下错误消息:

TypeError: render_page() takes exactly 2 arguments (1 given) 

我甚至不知道如何,因为我正在使用render_to_response()而不是直接调用render_page(),所以我认为这种事情会被处理掉?

views.py:

from django.http import HttpResponse,Http404
from django.template import Template,Context
from django.template.loader import get_template
from django.shortcuts import *

navs = {"About Me":"/aboutme/","Teaching":"/teaching/","CV":"/cv/","Online":"/epresence/",
"Links":"/links/","Main":"/main/"}
def render_page(request,page):
    if page == "epresence":
        return epresence(request)
    elif page == "links":
        return links(request)
    elif page == "teaching":
        return teaching(request)
    else:
        return other(request,page)
def epresence(request):
    pagetitle = "My Online Presence"
    sites = {...}
    navitems = globals().get("navs")
    return render_to_response("epresence.html",locals())
def links(request):
    pagetitle = "Links"
    navitems = globals().get("navs")
    friends = {...
}
    idols = {...
}
    resources = {...}
    return render_to_response("links.html",locals())
def teaching(request):
    pagetitle = "Teaching"
    navitems = globals().get("navs")
    teach = [...]
    learn = [...]
    return render_to_response("teaching.html",locals())
def other(request,page):
    pages = {"about":"About Me","me":"About Me","cv":"CV","home":"Home","main":"Home",}
    patetitle = pages.get(page)
    navitems = globals().get("navs")
    return render_to_response("main.html",locals())

urls.py:

from django.conf.urls import patterns, include, url
from personalsite.views import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^.*$',render_page),
)

有什么建议吗?

1 个答案:

答案 0 :(得分:4)

我不知道“我正在使用render_to_response()而不是直接调用render_page()”意思。你正在从你的urlconf中调用render_page - 但是,你没有传递任何URL,因为你没有在正则表达式中使用一个组。

您应该捕获这样的参数:

url(r'^(?P<page>.*)$',render_page),

(但是请注意,这是一件非常奇怪的事情.URLconf的重点是匹配特定的URL并调度到特定的视图函数:这样你就可以匹配urlconf中的epresence并得到它可以直接调用epresence功能。另外,你应该调查context processorsnavs dict传递给每个模板。)