3

Right now I have a pyramid (python) app with a home page that is reached via this route in the init.py file:

 config.add_route('home_page', '/')

in my views.py file I have:

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

and in my edit.pt template I have

<form action="${save_url}" method="post">
    <textarea name="name" tal:content="page.data" rows="10"
                    cols="60"/><br/>
    <textarea name="body" tal:content="page.name" rows="10"
                    cols="60"/><br/>
<input type="submit" name=form.submitted value="Save"/>
</form>

So basically the goal is to have the homepage show this edit.pt template which contains a form for submitting two pieces of information, a page name and page body. Upon submitting the form, the return HTTPFound function should redirect to the view_page created which shows the page name page body on a new permanent url.

I am not sure what I should add after the if statement in my home_page view_config. If the form hasn't been submitted I don't want anything to happen, it should just continue to show that edit.pt template. Right now I am getting an error when I try to visit the home page: ValueError: renderer was passed non-dictionary as value.

1 Answer 1

8

It looks like you are missing a condition

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

    # form was not submitted here, need to return context
    # ValueError: renderer was passed non-dictionary as value < beacuase you were
    # not returning context for this case before
    return {} # template contenxt here
3
  • so in the return function, do I put templates/edit.pt again even though I am already on that template? and Do I call it through return route.request_url('home_page',renderer='templates/edit.pt')
    – BigBoy1337
    Jan 29, 2013 at 0:55
  • @BigBoy1337, nope, templates/edit.pt should automatically be loaded with the values you are returning in the dictionary . you could type {'save_url': 'http://your_url.com'}
    – dm03514
    Jan 29, 2013 at 1:25
  • but your_url.com sounds like an independent domain from the rest of the app which is hosted at for ex: domain.com. I don't understand how it can do this. How can I generate a url at domain.com/custom_url?
    – BigBoy1337
    Jan 29, 2013 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.