0

我的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 {}        

此外,这里是 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>     

同样在我的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

4

3 回答 3

5

我认为您的表单应该提交给“/”,即。

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

有了之前的答案,现在看起来是正确的:

return HTTPFound(location=request.route_url('view_page', pagename=name))
于 2013-02-03T04:24:03.633 回答
0

从你给它的链接应该是

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

当您添加此路线时

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

并在之前设置变量名

    name= request.params['name']
于 2013-02-01T01:13:20.200 回答
0

我的第一个猜测是它location不是.LocationHTTPFound

于 2013-02-01T06:57:06.940 回答