如何结合 Django 的“LoginRequiredMixin”和表单工具的“FormPreview”?
How do I combine Django's ‘LoginRequiredMixin’ and Form Tools' ‘FormPreview’?
LoginRequiredMixin
在其他基于 class 的视图上效果很好。表单工具 FormPreview
也可以正常工作。但是当我尝试同时使用两者时,LoginRequiredMixin
会被忽略。
如果我使用form tools and the Django documentation中最基本的例子,情况也是如此。
有两件事我觉得很奇怪(但这不一定是原因):
- "This mixin should be at the leftmost position in the inheritance list." 这听起来像是有一些内部 hack,如果有人做了一些不正常的事情就会崩溃。没有解释为什么。
- 应该调用表单工具视图本身而不是方法
.as_view()
,这又是不合标准的。
这是一个错误吗?我能做什么?
FormPreview
不是基于 class 的通用视图,因此您不能将它与 LoginRequiredMixin
.
这样的混合使用
当您在 URL 模式中包含表单预览实例时,您可以使用 login_required
装饰器:
from django.contrib.auth.decorators import login_required
url_patterns = [
url(r'^form-handler/$', login_required(MyFormPreview(SomeModelForm))),
]
LoginRequiredMixin
在其他基于 class 的视图上效果很好。表单工具 FormPreview
也可以正常工作。但是当我尝试同时使用两者时,LoginRequiredMixin
会被忽略。
如果我使用form tools and the Django documentation中最基本的例子,情况也是如此。
有两件事我觉得很奇怪(但这不一定是原因):
- "This mixin should be at the leftmost position in the inheritance list." 这听起来像是有一些内部 hack,如果有人做了一些不正常的事情就会崩溃。没有解释为什么。
- 应该调用表单工具视图本身而不是方法
.as_view()
,这又是不合标准的。
这是一个错误吗?我能做什么?
FormPreview
不是基于 class 的通用视图,因此您不能将它与 LoginRequiredMixin
.
当您在 URL 模式中包含表单预览实例时,您可以使用 login_required
装饰器:
from django.contrib.auth.decorators import login_required
url_patterns = [
url(r'^form-handler/$', login_required(MyFormPreview(SomeModelForm))),
]