如何将基于函数的视图更改为基于类的视图

时间:2014-06-17 11:02:32

标签: django django-class-based-views

我正在使用Django 1.6.5。基本上我想要的是,当用户点击“删除”时,链接,没有显示任何形式。只需更新*_deleted_at字段,然后显示收件箱,而无需重定向到message_confirm_delete.html模板。

鉴于以下内容:

urls.py

...
url(r'^delete/(?P<message_id>[\d]+)/$', delete, name='messages_delete'),
...

views.py

def delete(request, message_id, success_url=None):
    user = request.user
    now = timezone.now()
    message = get_object_or_404(Message, id=message_id)
    deleted = False
    if success_url is None:
        success_url = reverse('messages:messages_inbox')
    if 'next' in request.GET:
        success_url = request.GET['next']
    if message.sender == user:
        message.sender_deleted_at = now
        deleted = True
    if message.recipient == user:
        message.recipient_deleted_at = now
        deleted = True
    if deleted:
        message.save()
        return HttpResponseRedirect(success_url)
    raise Http404

如何将以下基于函数的视图转换为基于类的视图?不确定哪一个适合此处:UpdateViewDeleteView

1 个答案:

答案 0 :(得分:0)

您也可以针对此案例使用RedirectView或使用DeletionMixin

的任何View类