将旧的基于函数的通用 CRUD 视图转换为新样式的基于类的通用 CRUD 视图
我在项目中的很多事情上都使用了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在深入研究 Django 源代码后,我找到了如何做到这一点。
After digging through the Django sources I found out how to do it.