How can I redirect after POST in Pyramid?

前端 未结 6 1551
花落未央
花落未央 2021-02-04 03:50

I\'m trying to have my form submit to a route which will validate the data then redirect back to the original route.

For example:

  • User loads the page websi
6条回答
  •  春和景丽
    2021-02-04 04:13

    Assuming your homepage is the default view of your pyramid web app, you can do:

    def _get_link_form(post_data):
        """ Returns the initialised form object """
    
        return LinkForm(post_data)
    
    def home_page(request):
    
        form = _get_link_form(request.POST)
        return {'form' : form}
    
    def save_post(request):   
        form = _get_link_form(request.POST)
    
        if not form.validate():
            from pyramid.httpexceptions import HTTPFound
            return HTTPFound(location=request.application_url)
    

    Basically you need to know how the home_page view was "added" to your Configurator. If your homepage actually lives at /few/levels/deep/homepage then a redirect might look like this:

            return HTTPFound(location=request.application_url + '/few/levels/deep/homepage')
    

提交回复
热议问题