Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top