gpt4 book ai didi

python - 如何创建 Python Pyramid View 类而不需要为每个方法指定 'name'

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:13 24 4
gpt4 key购买 nike

我的 Python Pyramid 项目中有几个 View 类是通过 add_handler 添加的:

config.add_handler('export_index', '/export', handler=ExportViews, action='index')

class ExportViews(ConfigViewBase):
@action(request_method='POST', name='index',
request_param='ftp_export.form.submitted')
@action(request_method='POST', name='index', xhr=True, renderer='json',
request_param='ftp_export.form.submitted')
def ftp_export(self):
#process form
return {}

@action(request_method='GET')
def index(self):
return {}

有没有可能做同样的事情:

config.add_handler('export_index', '/export', handler=ExportViews)

class ExportViews(ConfigViewBase):
@action(request_method='POST',
request_param='ftp_export.form.submitted')
@action(request_method='POST', xhr=True, renderer='json',
request_param='ftp_export.form.submitted')
def ftp_export(self):
#process form
return {}

@action(request_method='GET')
def __call__(self):
return {}

所以 __call__ 在浏览器获取页面时被调用,当我在同一页面上发布表单时应该调用 ftp_export 。现在我得到页面未找到错误

谢谢。

最佳答案

你可以通过遍历来做到这一点。遍历岩石:)

class Root(object):
def __getitem__(self, name):
if name == "export":
return ExportSomething(self)
if name == "export_something_else":
return ExportSomethingElse(self)



class ExportSomething(object):

implements(IViewable, IExportable)

def view(self, request):
return "Hi"

def export(self, request):
return "something else"

@view_config(context=IViewable, request_method="GET")
def view_viewable(conext, request):
return context.view(request)

@view_config(context=IExportable, request_method="POST")
def export_exportable(conext, request):
return context.export(request)

然后你可以实现一堆ExportThisExportThat类,让它们实现IViewableIExportable接口(interface),让它们从 Root.__getitem__ 返回,一切都神奇地工作。或者,如果您不需要多个导出器,您可以省略接口(interface)并将 View 直接绑定(bind)到 ExportSomething 类。或者您可以在 getitem 中实例化 ExportSomething 的不同实例,并使其...我不知道,查看/导出不同的文件/报告。

关于python - 如何创建 Python Pyramid View 类而不需要为每个方法指定 'name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11359722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com