I am looking for a way to redirect users to different routes/templates that is compatible with using @view_config.
I have a function that reads in an uploaded file and attempts to create a new model based on the file content. I was wondering if there was a clean way that I can redirect the user to one of two urls, based on whether the creation of the new model succeeds or there is an error.
If the model creation is successful, I want to redirect the user to the model page. If there is an error, I want to redirect the user to an error page. However, am having trouble breaking out the original function (load_model)’s view_config when rendering the error page.
@view_config(renderer="error.mak")
@view_config(renderer="model.mak",
route_name='load_model_route')
def load_model(self):
...
model = Model.find_model(model_name)
if model:
#redirect to model_route
else:
#redirect to model_error_route
Each route would have a @view_config that binds it to a function.
What you are asking is not “How to redirect” but “How to change renderer in the view function”. To answer quickly, I think you could use:
But I don’t think it’s a good idea. Here’s the usual pattern that is used most of the time to handle form submission:
In short:
GETon the route for a new model.POST) in a different view functionGETandPOST, but the one in thePOSTis only used in case of invalid data (otherwise, you redirect).