Question

I'm using pylons, and my action of controller is:

 class UserController(BaseController):

      def create(self):
           name = request.POST['name']
           email = request.POST['email']
           password = request.POST['password']
           ...

But I found in turbogears, I can do like this:

 class UserController:

      def create(self, name, email, password):
           ...

How to do the same in pylons?


UPDATE

I've searched in google, and asked in some other forums, but still not get the answer. Nobody knows(or interested in) such a question?

Was it helpful?

Solution

It sounds like you want to give arguments to the create() method of your controller that are derived directly from elements of the POST data. You can do this, but it's rather fiddly.

The shortest way to do it - a fragile way - would be to just define the action like so, using those environment variables as defaults for the action.

class UserController(Base):
  def create(self, name=request.POST['name'], email=request.POST['email'], ...):
        ...

The thing is that even though it may appear cumbersome, the first method you showed probably is a better way to do it, because it gives you more room to recover gracefully from those variables not being what you expect them to be.

If you feel like getting really fiddly, you can push this logic into routing.py. The POST data is already available there, just not as directly, because logic like this belongs in your controller. You would use a conditional method in Routes, which gives you access to environ[wsgi.input], which has the POST data, then extract your desired data from there, and push it into the match_dict dictionary, which in turn would let you feed the POSTDATA directly to your controller action's arguments. Here's the Pylons Book section on conditional functions in routing.py, and here's a similar question here on SO and another, which should help if you really want to work with wsgi.input directly.

However, instead of either of those things, I would use your original method:

class UserController(BaseController):
    def create(self):
       name = request.POST['name']
       email = request.POST['email']
       password = request.POST['password']
       ...

It's definitely possible to parse the POST data before it gets to your controller and to take actions on it before then. However, looking at POST data and deciding what to do with it is a controller's role, not Routes' role or a middleware role. If you decide to - for example - start fiddling with the POST data in routing.py, you're losing some of the loosely-coupled advantages of Pylons and to a smaller extent the whole WSGI shebang.

You will see Pylons controllers that look like this, though:

class UserController(BaseController):
    def create(self, name, spam):
       ...

However, in those cases, the values of "name" and "spam" come from the query string (and from Routes' map), not from the POST data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top