Pyramid 应用程序:如何将值传递到我的 request.route_url 中? [英] Pyramid app: How can I pass values into my request.route_url?

查看:27
本文介绍了Pyramid 应用程序:如何将值传递到我的 request.route_url 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 views.py 文件中将此作为主页的视图配置:

@view_config(route_name='home_page', renderer='templates/edit.pt')定义主页(请求):如果 request.params 中的form.submitted":name= request.params['name']body = request.params['body']页=页(名称,正文)DBSession.add(页面)返回 HTTPFound(location=request.route_url('view_page',pagename=name))返回 {}

此外,这里是edit.pt模板中的表单:

<div><input type="text" name="name"/>

<div><input type="text" name="body"/>

<label for="stl">Stl</label><input name="stl" type="file" value=""/><input type="submit" name='form.submitted' value="保存"/></表单>

也在我的 init.py 文件中我有

 config.add_route('home_page', '/')config.add_route('view_page', '/{pagename}')

现在,当我提交表单时,它只是尝试转到 localhost:6543/view_page.这将返回 404,因为没有 view_page 资源或通往它的路由.相反,我希望它转到 localhost:6543/(我刚刚创建的页面名称,也就是表单中的第一个输入框).我怎样才能做到这一点?

我担心其他东西可能会告诉它路由到view_page,因为我什至尝试将其更改为

return HTTPFound(location=request.route_url('front_page',pagename=name))

它仍然转到/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}'))

另外,我正在研究这篇文章,它似乎对语法很有帮助:http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/urldispatch.html#urldispatch-chapter

解决方案

我认为您的表单应该提交到/",即.

<!-- 您的 home_page 路由正在等待 POST --><form action="/" method="post">

根据之前的答案,现在看起来是正确的:

return HTTPFound(location=request.route_url('view_page', pagename=name))

I have this in my views.py file as the view config for my home page:

@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 {}        

Also, here is the form in the edit.pt template:

<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>     

Also in my init.py file I have

    config.add_route('home_page', '/')
    config.add_route('view_page', '/{pagename}')

right now when I submit the form it just tries to go to localhost:6543/view_page. This returns a 404 as there is no view_page resource or route leading to it. Instead I want it to go to localhost:6543/(the name of the page I just created aka the first input box in the form). How can I do this?

Edit: I am worried that something else may be telling it to route to view_page because I even tried changing it to

return HTTPFound(location=request.route_url('front_page',pagename=name))

And it still goes to /view_page. There is no route named front_page, so I would at least suspect it to throw an error.

Also, I would really appreciate it if you could tell me where you found the info. I have been looking at http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/api/request.html?highlight=request.route_url#pyramid.request.Request.route_url but can't seem to find use from it.

Edit: should I be using an asset specification instead of a path name? so

return HTTPFound(Location=request.route_url('tutorial:templates/view.pt','/{pagename}'))

Also, I am working through this article which seems very helpful with the syntax: http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/urldispatch.html#urldispatch-chapter

解决方案

I think your form should submit to "/", ie.

<!-- where your home_page route is waiting for the POST -->
<form action="/" method="post">

With the prior answers this now looks correct:

return HTTPFound(location=request.route_url('view_page', pagename=name))

这篇关于Pyramid 应用程序:如何将值传递到我的 request.route_url 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆