将旧的基于函数的通用 CRUD 视图转换为新样式的基于类的通用 CRUD 视图

发布于 2024-12-05 04:52:25 字数 1138 浏览 2 评论 0原文

我在项目中的很多事情上都使用了 Django 的通用 CRUD 视图。我现在想开始迁移到 DJango 1.3 中新型基于类的通用 CRUD 视图。我没有发现这些文档有很大帮助,并且可以在转换我的一些基于函数的视图时使用一些帮助。这是我的视图片段:

def create_domains(request, *args, **kwargs):
    return create_object(request=request, form_class=DomainForm,
        template_name='customer/create_domains.html',
        post_save_redirect=reverse('manage_domains')
    )

def delete_domains(request, object_id, *args, **kwargs):
    return delete_object(request=request,
        object_id=object_id, model=Domain,
        template_name='customer/delete_domains.html',
        post_delete_redirect=reverse('manage_domains')
    )

我想我将在第一个视图中使用 CreateView ,在下一个视图中使用 DeleteView 。这是我到目前为止所想到的:

class DomainCreateView(CreateView):
    form_class = DomainForm
    template_name = 'create_domains.html'
    success_url = 'manage_domains'

class DomainDeleteeView(CreateView):
    model = Domain
    template_name = 'delete_domains.html'
    success_url = 'manage_domains'
    pk_url_kwarg = object_id

你们中的一个人可以告诉我重写相同的基于类的视图吗?一点点的起步将会有很大的帮助,我相信我可以从那里开始。

谢谢。

I've been using Django's generic CRUD views for quite a few things in my project. I'd now like to begin migrating to new style class-based generic CRUD views in DJango 1.3. I didn't finds the docs to be very help and could use a little help with converting some of my function-based views. Here's a snippet of my views:

def create_domains(request, *args, **kwargs):
    return create_object(request=request, form_class=DomainForm,
        template_name='customer/create_domains.html',
        post_save_redirect=reverse('manage_domains')
    )

def delete_domains(request, object_id, *args, **kwargs):
    return delete_object(request=request,
        object_id=object_id, model=Domain,
        template_name='customer/delete_domains.html',
        post_delete_redirect=reverse('manage_domains')
    )

I guess I'll be using a CreateView for the first one and a DeleteView for the next one. This is what I've come up with so far:

class DomainCreateView(CreateView):
    form_class = DomainForm
    template_name = 'create_domains.html'
    success_url = 'manage_domains'

class DomainDeleteeView(CreateView):
    model = Domain
    template_name = 'delete_domains.html'
    success_url = 'manage_domains'
    pk_url_kwarg = object_id

Could one of you show me to rewrite the same a class-based view? A little jump start would be a great help and I'm confident I could take it from there on.

Thanks.

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

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

发布评论

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

评论(1

明媚殇 2024-12-12 04:52:25

在深入研究 Django 源代码后,我找到了如何做到这一点。

class DomainCreateView(CreateView):
    """
    Creates a Domain
    """
    form_class = DomainForm
    template_name = 'customer/create_domains.html'
    success_url = reverse_lazy('manage_domains')

    @method_decorator(login_required)
    @method_decorator(only_managers_allowed)
    def dispatch(self, *args, **kwargs):
        """
        The Dispatcher
        """
        return super(DomainCreateView, self).dispatch(*args, **kwargs)

    def form_valid(self, form):
        """
        Validate and save the form
        """
        company = self.request.user.get_profile().company
        self.object = form.save(company)
        return super(ModelFormMixin, self).form_valid(form)


class DomainDeleteView(DeleteView):
    """
    Deletes a Domain
    """
    model = Domain
    template_name = 'customer/delete_domains.html'
    success_url = reverse_lazy('manage_domains')

    @method_decorator(login_required)
    @method_decorator(only_managers_allowed)
    def dispatch(self, *args, **kwargs):
        """
        The Dispatcher
        """
        return super(DomainDeleteView, self).dispatch(*args, **kwargs)

After digging through the Django sources I found out how to do it.

class DomainCreateView(CreateView):
    """
    Creates a Domain
    """
    form_class = DomainForm
    template_name = 'customer/create_domains.html'
    success_url = reverse_lazy('manage_domains')

    @method_decorator(login_required)
    @method_decorator(only_managers_allowed)
    def dispatch(self, *args, **kwargs):
        """
        The Dispatcher
        """
        return super(DomainCreateView, self).dispatch(*args, **kwargs)

    def form_valid(self, form):
        """
        Validate and save the form
        """
        company = self.request.user.get_profile().company
        self.object = form.save(company)
        return super(ModelFormMixin, self).form_valid(form)


class DomainDeleteView(DeleteView):
    """
    Deletes a Domain
    """
    model = Domain
    template_name = 'customer/delete_domains.html'
    success_url = reverse_lazy('manage_domains')

    @method_decorator(login_required)
    @method_decorator(only_managers_allowed)
    def dispatch(self, *args, **kwargs):
        """
        The Dispatcher
        """
        return super(DomainDeleteView, self).dispatch(*args, **kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文