[mod_python] some questions about using mod_python

Graham Dumpleton grahamd at dscpl.com.au
Sun Mar 20 16:30:54 EST 2005


On 20/03/2005, at 9:55 PM, Graham Dumpleton wrote:

>> - I want to use a MVC approach,the publisher's methods are the 
>> controlers
>> that do the processing and send internal redirects to psps to show the
>> results,so i need to pass objects to the psps from the pub methods,i 
>> need
>> those objects to be in the request object of the handler and 
>> available to
>> the target psp.
>
> Why do you need to redirect the request to PSP? Why couldn't you simply
> write a common method of your own which triggered PSP page rendering
> directly within your publisher method with the desired environment?
>
> At its simplest, you could use:
>
>     template = psp.PSP(req,filename=path,vars=settings)
>     template.run()
>
> Where "path" is the name of the PSP file and "settings" is a dictionary
> populated with data that your controller has obtained from the model.
> Using redirection seems to me to be drawing too much of an artificial
> separation between your controller and view.

Another issue that you should be mindful of when using internal 
redirects
is that POST form parameters do not survive an internal redirect. Your
controller code which does the internal redirect would need to process
any POST form parameters and work out what to do with them. There would
be no means of communicating any result of this processing through to 
the
PSP page though. The only thing which will get transfered through to the
PSP pages is the original GET form parameters.

If you follow the recipe above and use psp.PSP() direct, you still hit a
similar problem in that psp.PSP() ignores any req.form data which is 
left
there by publisher and instead tries to process form parameters again.
Thus, any POST form parameters are still not available. I have posted
that this could possibly be improved by having psp.PSP() look for the
req.form from a prior parsing of the form parameters.

   http://issues.apache.org/jira/browse/MODPYTHON-38

When using psp.PSP() direct, you have a few options for making use of 
forms
when triggered from inside publisher. The first is that the controller
code in the publisher function translates them all into "vars" settings
supplied to psp.PSP(). Alternatively, a PSP page rather than accessing
the form through the "form" variable, instead uses "req.form" which will
access directly the FieldStorage instance left by the original publisher
function.

The best workaround though would probably be to use:

     settings["form"] = req.form
      template = psp.PSP(req,filename=path,vars=settings)
      template.run()

The caller setting of "form" here takes precedence over that put into 
the
variable set by PSP. The PSP pages can then still use "form". The only
downside of this is that it doesn't eliminate the redundant processing
of form parameters that occurs because "form" is referenced in the PSP 
page.

Graham








More information about the Mod_python mailing list