我有以下视图代码:
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()
基本上,它检查表中是否存在页面。如果有的话
根据路径名渲染模板,或者如果没有这样的模板
模板,它只是呈现一个默认模板。
接下来要做的是为我的视图创建一个单独的视图函数
“博客”页面,此页面具有一些逻辑。
我已经尝试过以下示例,但会引发未定义的错误
当我加载页面时:
@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/