BigBoy1337
BigBoy1337

Reputation: 5023

Pyramid app: How can I pass values into my request.route_url?

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

Upvotes: 0

Views: 4173

Answers (3)

Ian Wilson
Ian Wilson

Reputation: 9099

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

Upvotes: 5

Paul Yin
Paul Yin

Reputation: 1759

from the link you give it's should be

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

when you had add this route

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

and set the variable name before

    name= request.params['name']

Upvotes: 0

Michael Merickel
Michael Merickel

Reputation: 23331

My first guess is that it's location not Location as the argument to HTTPFound.

Upvotes: 0

Related Questions