-2

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

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

我什至不知道如何,因为我使用的是 render_to_response() 而不是直接调用 render_page() ,所以我认为这种事情会得到处理?

视图.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())

网址.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),
)

有什么建议么?

4

1 回答 1

4

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

您应该像这样捕获参数:

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

(但请注意,这是一件相当奇怪的事情。URLconf 的全部目的是匹配特定的 URL 并分派到特定的视图函数:这样您epresence就可以在 urlconf 中匹配并让它直接调用 epresence 函数。此外,您应该查看上下文处理器以将您的navsdict 传递给每个模板。)

于 2012-08-08T21:04:09.047 回答