Pyramid Framework - 帮助创建视图函数
我有以下视图代码:
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_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
I have the following code for the 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()
Basically, it checks if a page exists in the table. If it does it
renders a template according to the path name, or if there is no such
template it just renders a default template.
The next part I want to do is create a seperate view function for my
'blog' page, with some logic for this page.
I've tried the following example, but it throws an undefined error
when when I load the page:
@view_config(renderer='myblog:templates/pages/my-blog.mak')
def blog_view(request):
one = 'Hello World'
return {'one':one}
I do apologise for such basic questions. Any insight given will be
greatly appreciated.
Here is the traceback:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你最好继续解决这个问题,而不是开始另一个问题。基于此处和 您在其他问题中提供的评论,正在发生以下情况:
查看:
您的“page.mak”模板文件中有以下内容...
${args2}
当 view1 被调用时,render_to_response 尝试渲染 page.mak 模板。字典
{'args1'=args1}
用作上下文。当 mako 看到${args2}
时,它会在上下文字典中查找args2
。 view1中构造的上下文字典在上下文字典中没有args2
,所以会导致错误。在 view2 中渲染同一页面效果很好,因为 view2 使用键args2
正确传递了预期值。因此,您收到的错误是因为尝试在模板中呈现从未放入模板上下文字典中的变量。
You would have been better off following through on this question rather than starting the other one. Based on information here and in the comment you provided in the other question, the following is taking place:
View:
You have the following in your 'page.mak' template file...
${args2}
When view1 is called, render_to_response tries to render the page.mak template. The dictionary
{'args1'=args1}
is used as context. When mako sees${args2}
, it looks upargs2
in the context dictionary. The context dictionary constructed in view1 does not haveargs2
in the context dictionary, so it will cause an error. Rendering this same page in view2 works fine, because view2 is correctly passing the expected value with the keyargs2
.So, the error you're getting is because of trying to render a variable in your template that you never placed into the template context dictionary.
您的路由系统似乎存在配置问题,请尝试
将操作更改为此,如果有效,可能是权限问题,
可以发布错误引发的堆栈跟踪吗?
It look's like you have configuration issues in your routing system, try
to change the action to this, if it works maybe its a permission issue,
could post the stack trace that the error raise?
取出代码的第一部分,解决了错误。
也许是因为 my-blog.mak 的 render_to_response 发生了两次?
在代码的第一部分中,
如果 my-blog.mak 存在,则渲染它,
我第二次这样做:
问题是我需要先渲染模板(如果它们存在或不存在)并且位于数据库表中。
第二部分是为该特定模板创建一些单独的逻辑。
Taking out the first part of the code, resolves the error.
Perhaps because render_to_response for my-blog.mak is occuring twice?
In the first part of the code you have
Which renders my-blog.mak if it exists,
The second time I do:
The thing is I need something first renders the templates if they exist or not and are in the database table.
The second part is creating some seperate logic for that particular template.
正如马克·希尔德雷斯 (Mark Hildreth) 在您最初问题的评论中所说,该错误似乎是在模板渲染期间发生的。您可能尝试使用视图函数中不存在/未设置的变量执行某些操作。
在第一个示例中,您总是设置一个
page
变量,也许这就是缺少的变量。Like Mark Hildreth said in the comments of your original question, the error seems to be happening during the rendering of your template. You probably try to do something with a variable that does not exist/is not set in your view function.
In your first example, you always set a
page
variable, maybe that's the one missing.