我怎样才能为新的 Django 表单向导提供单例视图?
我正在使用新的 Django 表单向导工具。它将在下一个 Django 1.4 中发布,但您可以在这里找到它: https://github.com/ stephrdev/django-formwizard
我想为所有向导过程提供一个单例视图类。
这是我的代码:
class submit(object):
instance = None
def __new__(cls, request, *args, **kwargs):
if not cls.instance:
cls.instance = super(submit, cls).__new__(cls)
cls.form = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])
return cls.instance(request, *args, **kwargs)
def __init__(self):
pass
def __call__(self, request, *args, **kwargs):
return self.form(request)
问题是 WizardView 继承自 TemplateView,因此 as_view
方法返回一个函数。所以,在这行中:
cls.form = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])
就像我在类变量中存储了一个函数。因此,在 __call__ 方法中,当我调用 self.form 函数时,Python 会自动添加该类的引用作为第一个参数。 这是我不想要的。
我该如何解决它?有什么想法吗?
抱歉我的英语:S
提前致谢,
问候!
I am working with the new Django Form Wizard tool. It will be released in the next Django 1.4, but you can find it here: https://github.com/stephrdev/django-formwizard
I would like to have a singleton view class for all the wizard process.
This is my code:
class submit(object):
instance = None
def __new__(cls, request, *args, **kwargs):
if not cls.instance:
cls.instance = super(submit, cls).__new__(cls)
cls.form = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])
return cls.instance(request, *args, **kwargs)
def __init__(self):
pass
def __call__(self, request, *args, **kwargs):
return self.form(request)
The problem is that WizardView inherits from TemplateView so the as_view
method returns a function. So, in the line:
cls.form = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])
It's like I was storing in a class variable a function. So, in the __call__
method, when I call to self.form function, Python automatically add as the first parameter a reference of the class. This is what I do not want.
How could I resolve it? Any ideas?
Sorry for my english :S
Thanks in advance,
Greetings!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的问题。我还没有测试过,但这个修改应该有效:
从:
到:
Interesting issue. I haven't tested, but this modification should work:
from:
to: