gpt4 book ai didi

python - Pyramid Framework - 帮助创建 View 函数

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:47 34 4
gpt4 key购买 nike

我有以下 View 代码:

def pages_view(request):
path = request.path.split('/')[1]
dbsession = DBSession()
page = dbsession.query(PagesTable).from_statement(
'SELECT * FROM pages WHERE path=:page_path').params(page_path=path).first()
pages_dir = os.getcwd() + '/myblog/templates/pages/'
if page:
if path == '':
return render_to_response('myblog:templates/pages/home.mak',
{'page':page}, request=request)
elif os.path.isfile(pages_dir + path + '.mak'):
return render_to_response('myblog:templates/pages/%s.mak'%path,
{'page':page}, request=request)
else:
return render_to_response('myblog:templates/pages/index.mak',
{'page':page}, request=request)
raise NotFound()

基本上,它检查表中是否存在页面。如果它做到了根据路径名呈现模板,或者如果没有这样的template 它只是呈现一个默认模板。

我想做的下一部分是为我的创建一个单独的 View 函数“博客”页面,包含此页面的一些逻辑。

我试过下面的例子,但是它抛出了一个未定义的错误当我加载页面时:

@view_config(renderer='myblog:templates/pages/my-blog.mak')
def blog_view(request):
one = 'Hello World'
return {'one':one}

对于这些基本问题,我深表歉意。给出的任何见解将是非常感谢。

这是回溯:

URL: http://127.0.0.1:6543/my-blog/
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/WebError-0.10.3-py2.6.egg/weberror/evalexception.py', line 431 in respond
app_iter = self.application(environ, detect_start_response)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/repoze.tm2-1.0b1-py2.6.egg/repoze/tm/__init__.py', line 23 in __call__
result = self.application(environ, save_status_and_headers)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/router.py', line 158 in __call__
response = view_callable(context, request)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/config.py', line 2916 in _requestonly_view
response = view(request)
File '/Users/Awais/virtualenv/MyBlog/myblog/views.py', line 25 in pages_view
{'page':page}, request=request)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/renderers.py', line 111 in render_to_response
return helper.render_to_response(value, None, request=request)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/renderers.py', line 322 in render_to_response
result = self.render(value, system_values, request=request)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/renderers.py', line 318 in render
result = renderer(value, system_values)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/mako_templating.py', line 131 in __call__
result = template.render_unicode(**system)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/template.py', line 292 in render_unicode
as_unicode=True)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/runtime.py', line 575 in _render
**_kwargs_for_callable(callable_, data))
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/runtime.py', line 607 in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/runtime.py', line 633 in _exec_template
callable_(context, *args, **kwargs)
File 'myblog_templates_pages____base_mak', line 27 in render_body
File 'myblog_templates_pages_my_blog_mak', line 34 in render_body
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/runtime.py', line 181 in __str__
raise NameError("Undefined")
NameError: Undefined

最佳答案

你最好完成这个问题而不是开始另一个问题。基于此处和 comment you provided in the other question 中的信息, 正在发生以下情况:

查看:


def view1(request)
args1 = 'a string'
return render_to_response('page.mak',{'args1':args1}, request=request)

def view2(request)
args2 = 'a string 2'
return render_to_response('page.mak',{'args2':args2}, request=request)

您的“page.mak”模板文件中包含以下内容...

${args2}

调用 view1 时,render_to_response 会尝试呈现 page.mak 模板。字典 {'args1'=args1} 用作上下文。当 mako 看到 ${args2} 时,它会在上下文字典中查找 args2。 view1中构造的上下文字典,上下文字典中没有args2,所以会报错。在 view2 中呈现相同的页面工作正常,因为 view2 正确传递了键 args2 的预期值。

所以,你得到的错误是因为试图在你的模板中渲染一个你从未放入模板上下文字典中的变量。

关于python - Pyramid Framework - 帮助创建 View 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5282786/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com