使用 mixin 和 def
using mixin with a def
我正在 generic.views 上使用这个 Accessmixin:
class OrganisorAndLoginRequiredMixin(AccessMixin):
"""Verify that the current user is authenticated and is an organisor."""
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated or not request.user.is_organisor:
return redirect("home")
return super().dispatch(request, *args, **kwargs)
我想在 def 上使用它。我试过了,但出现以下错误。
@OrganisorAndLoginRequiredMixin()
def AgentUpdateView(request, pk):
obj = Agent.objects.filter(pk=pk).first()
if obj is None:
return render(request, "404.html", {
"Instance": pk
})
else:
form = AgentModelForm(instance=obj.user)
错误:
类型错误:'OrganisorAndLoginRequiredMixin' 对象不可调用
你需要为此使用 class based view:
from django.views.generic.edit import UpdateView
class AgentUpdateView(OrganisorAndLoginRequiredMixin, UpdateView):
model = Agent
template_name = 'template.html'
form_class = AgentModelForm
我正在 generic.views 上使用这个 Accessmixin:
class OrganisorAndLoginRequiredMixin(AccessMixin):
"""Verify that the current user is authenticated and is an organisor."""
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated or not request.user.is_organisor:
return redirect("home")
return super().dispatch(request, *args, **kwargs)
我想在 def 上使用它。我试过了,但出现以下错误。
@OrganisorAndLoginRequiredMixin()
def AgentUpdateView(request, pk):
obj = Agent.objects.filter(pk=pk).first()
if obj is None:
return render(request, "404.html", {
"Instance": pk
})
else:
form = AgentModelForm(instance=obj.user)
错误: 类型错误:'OrganisorAndLoginRequiredMixin' 对象不可调用
你需要为此使用 class based view:
from django.views.generic.edit import UpdateView
class AgentUpdateView(OrganisorAndLoginRequiredMixin, UpdateView):
model = Agent
template_name = 'template.html'
form_class = AgentModelForm