如何让action支持更多参数?

发布于 2024-09-18 06:21:52 字数 510 浏览 7 评论 0原文

我正在使用塔架,我的控制器动作是:

 class UserController(BaseController):

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

但我发现在涡轮齿轮中,我可以这样做:

 class UserController:

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

如何在塔架中做同样的事情?


更新

我在谷歌上搜索过,并在其他一些论坛上询问过,但仍然没有得到答案。没有人知道(或感兴趣)这样的问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个ˇ局外人 2024-09-25 06:21:53

听起来您想为控制器的 create() 方法提供直接从 POST 数据元素派生的参数。你可以这样做,但它相当繁琐。

最短的方法(一种脆弱的方法)就是像这样定义操作,使用这些环境变量作为操作的默认值。

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

问题是,尽管它可能看起来很麻烦,但您展示的第一种方法可能是更好的方法,因为它为您提供了更多的空间,可以从那些与您期望的不同的变量中优雅地恢复。

如果您觉得非常麻烦,可以将此逻辑推入routing.py。 POST 数据已经在那里可用,只是不那么直接,因为这样的逻辑属于您的控制器。您可以在路由中使用条件方法,这样您就可以访问environ[wsgi.input],其中有 POST 数据,然后从那里提取所需的数据,并将其推送到 match_dict 字典中,这反过来又可以让您将 POSTDATA 直接提供给控制器操作的参数。 这是关于路由中条件函数的 Pylons Book 部分 .py,这里有一个关于SO的类似问题另一个,如果您确实想直接使用 wsgi.input ,这应该会有所帮助。

但是,我不会使用这些方法中的任何一个,而是使用您原来的方法:

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

绝对可以在 POST 数据到达控制器之前对其进行解析,并在此之前对其采取操作。然而,查看 POST 数据并决定如何处理它是控制器的角色,而不是路由的角色或中间件的角色。例如,如果您决定开始摆弄routing.py 中的POST 数据,您就会失去Pylons 的一些松散耦合优势,并在较小程度上失去整个WSGI shebang。

不过,您将看到像这样的 Pylons 控制器:但是

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

,在这些情况下,“name”和“spam”的值来自查询字符串(以及来自 Routes 的映射),而不是来自POST 数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文