Big*_*337 0 python routing pyramid
我在views.py文件中将此作为我的主页的视图配置:
@view_config(route_name='home_page', renderer='templates/edit.pt')
def home_page(request):
if 'form.submitted' in request.params:
name= request.params['name']
body = request.params['body']
page=Page(name,body)
DBSession.add(page)
return HTTPFound(location=request.route_url('view_page',pagename=name))
return {}
Run Code Online (Sandbox Code Playgroud)
此外,这是edit.pt模板中的表单:
<form action="/view_page" method="post">
<div>
<input type="text" name="name"/>
</div>
<div>
<input type="text" name="body"/>
</div>
<label for="stl">Stl</label>
<input name="stl" type="file" value="" />
<input type="submit" name='form.submitted' value="Save"/>
</form>
Run Code Online (Sandbox Code Playgroud)
我也在我的init .py文件中
config.add_route('home_page', '/')
config.add_route('view_page', '/{pagename}')
Run Code Online (Sandbox Code Playgroud)
现在,当我提交表单时,它只是尝试转到localhost:6543/view_page.这将返回404,因为没有view_page资源或通向它的路由.相反,我希望它转到localhost:6543 /(我刚刚创建的页面的名称,也就是表单中的第一个输入框).我怎样才能做到这一点?
编辑:我担心其他东西可能会告诉它路由到view_page,因为我甚至尝试将其更改为
return HTTPFound(location=request.route_url('front_page',pagename=name))
Run Code Online (Sandbox Code Playgroud)
它仍然转到/ view_page.没有名为front_page的路由,所以我至少会怀疑它会抛出错误.
另外,如果你能告诉我你在哪里找到这些信息,我将非常感激.我一直在关注http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/api/request.html?highlight=request.route_url#pyramid.request.Request.route_url但似乎无法从中找到用处.
编辑:我应该使用资产规范而不是路径名吗?所以
return HTTPFound(Location=request.route_url('tutorial:templates/view.pt','/{pagename}'))
Run Code Online (Sandbox Code Playgroud)
另外,我正在阅读这篇文章,它似乎对语法非常有帮助:http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/urldispatch.html#urldispatch-chapter
我认为你的表格应该提交到"/",即.
<!-- where your home_page route is waiting for the POST -->
<form action="/" method="post">
Run Code Online (Sandbox Code Playgroud)
通过先前的答案,现在看起来是正确的:
return HTTPFound(location=request.route_url('view_page', pagename=name))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3575 次 |
最近记录: |