如何在django-reversion app中创建REVERT机会?

时间:2017-12-04 10:48:11

标签: python django django-views django-class-based-views django-reversion

我有责任将正确的Function Based View重写为Class Based View。您可以在下面看到我的代码。我的问题是我的基于类的视图有什么问题?它会引发错误。我的错误在哪里?

FBV:

@reversion.create_revision()
def article_revert(request, pk, article_reversion_id):
    article = get_object_or_404(Article, pk=pk)
    revision = get_object_or_404(Version.objects.get_for_object(article), pk=article_reversion_id).revision
    reversion.set_user(request.user)
    reversion.set_comment("REVERT to version: {}".format(revision.id))
    revision.revert()
    return redirect('project:article_list')

CBV:

class ArticleRevert(RevisionMixin, View):
    model = Article

    def get(self, request, *args, **kwargs):
        article = get_object_or_404(Article, pk=pk)
        revision = get_object_or_404(Version.objects.get_for_object(article), pk=article_reversion_id).revision
        reversion.set_comment("REVERT to version: {}".format(revision.id))
        revision.revert()
        return redirect('project:article_list')

当我使用CBV时会引发下一个错误:

Traceback (most recent call last):
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/reversion/views.py", line 43, in do_revision_view
    return func(request, *args, **kwargs)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Applications/Projects/web/project/article/views.py", line 166, in get
    reversion.set_comment("REVERT to version: {}".format(revision.id))
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/reversion/revisions.py", line 122, in set_comment
    _update_frame(comment=comment)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/reversion/revisions.py", line 87, in _update_frame
    _local.stack = _local.stack[:-1] + (_current_frame()._replace(**kwargs),)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/reversion/revisions.py", line 53, in _current_frame
    raise RevisionManagementError("There is no active revision for this thread")
RevisionManagementError: There is no active revision for this thread

0 个答案:

没有答案