I am attempting to implement a pattern where I use an intermediate function to determine which function to call and have the final function do the rendering, but the flow does not end up rendering. What am I missing? Is there a way to tweak this to make it work?
Here’s what I’m attempting.
@view_config(route='fork_route')
def fork(self):
x = True
if x:
self.my_func1
else:
self.my_func2
#I expected it to render before this point
return dict({'msg':'failed'})
@view_config(renderer="templates/derived/template1")
def my_func1:
return dict({'msg':'msg1'})
@view_config(renderer="templates/derived/template2")
def my_func2:
return dict({'msg':'msg2'})
Pyramid’s renderers are pretty simple, and in the end it’s “just python”. Meaning you’re calling functions from functions, nothing special.
The way Pyramid’s automated rendering works is:
The
view_configis only relevant to the current request’s view. The other functions you are calling are simply functions in Python.If you still want to delegate the work to another view then there are several options, but the one we tell people is that you will need to explicitly call
pyramid.renderers.renderwithin the sub-view orrender_to_response. Of course if you only callrenderthen you must turn that html body into a fullResponseobject.Notice how
myfunc1returns aResponseobject sofork(the view that is active for this request) can just return it.Otherwise you need to turn the result into a response:
There are obviously benefits and caveats to each approach.