【问题标题】:Django Call Class based view from another class based view从另一个基于类的视图中调用基于类的视图
【发布时间】:2013-02-04 02:04:26
【问题描述】:

我正在尝试调用基于类的视图并且我能够做到,但由于某种原因我没有得到我正在调用的新类的上下文

class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView):
    template_name = "accounts/thing.html"



    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(ShowAppsView, self).dispatch(*args, **kwargs)

    def get(self, request, username, **kwargs):
        u = get_object_or_404(User, pk=self.current_user_id(request))

        if u.username == username:
            cities_list=City.objects.filter(user_id__exact=self.current_user_id(request)).order_by('-kms')
            allcategories = Category.objects.all()
            allcities = City.objects.all()
            rating_list = Rating.objects.filter(user=u)
            totalMiles = 0
            for city in cities_list:
                totalMiles = totalMiles + city.kms

        return self.render_to_response({'totalMiles': totalMiles , 'cities_list':cities_list,'rating_list':rating_list,'allcities' : allcities, 'allcategories':allcategories})


class ManageAppView(LoginRequiredMixin, CheckTokenMixin, CurrentUserIdMixin,TemplateView):
    template_name = "accounts/thing.html"

    def compute_context(self, request, username):
        #some logic here                        
        if u.username == username:
            if request.GET.get('action') == 'delete':
                #some logic here and then:
                ShowAppsView.as_view()(request,username)

我做错了什么?

【问题讨论】:

  • 这应该是做什么的?您希望通过简单地调用该视图来实现什么?我猜您可能需要返回调用它的结果,但由于compute_context 是一种非标准方法,因此很难确定。
  • 我有点“刷新”我的页面,所以我用一些新的上下文数据回忆我的上一页
  • 我正在返回 return self.render_to_response(self.compute_context(request,username))
  • 但是您还没有通过简单地调用该视图来解释您希望发生的事情。你调用它然后丢弃响应:正如我所说,你真的想返回它吗?或者,也许您打算调用它的 compute_context 方法并使用它返回的值做一些事情?事实上,这条线完全没有意义。
  • 因为你调用它然后对结果不做任何事情

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


【解决方案1】:

当你开始在 python 中使用multiple inheritance 时,事情会变得更加复杂,所以你很容易用继承的 mixin 来践踏你的上下文。

你并没有完全说出你得到了哪个上下文以及你想要哪个(你没有定义一个新的上下文),所以很难完全诊断,但是尝试重新排列你的 mixin 的顺序;

class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView):

这意味着LoginRequiredMixin 将是第一个继承自的类,因此如果它具有您要查找的属性,它将优先于其他类 - 如果没有,则 python 将查找 @987654324 @ 等等。

如果您想真正确定获得所需的上下文,可以添加类似

的覆盖
def get_context(self, request):
    super(<my desired context mixin>), self).get_context(request)

以确保您获得的上下文是您想要的来自 mixin 的上下文。

* 编辑 * 我不知道你在哪里找到 compute_context 但它不是 django 属性,所以只会从 ShowAppsView.get() 调用,而不会在 ManageAppView 调用。

【讨论】:

  • 我在上面编辑了我的代码并取出了 compute_context 但它仍然无法正常工作。我应该从 ShowAppSview 继承 ManageAppView 以访问该方法吗?
  • 同上@Daniel Roseman 如果compute_context 是您想要返回的,那么您将需要它。它是非标准的,所以可能应该在 get_context 或类似的。我提供的不是完整的解决方案,而是探索/调查的路线。
【解决方案2】:

代替

ShowAppsView.as_view()(self.request)

我不得不这样做

return ShowAppsView.as_view()(self.request)

【讨论】:

  • 我发现如果你执行 ShowAppsView.as_view()(request, *args, **kwargs) 实际上可以通过 ContextMixin 将 args 和 kwargs 传递给 get_context_data 方法,它们在哪里显示为 self.args 和 self.kwargs。这对于覆盖此方法并添加到例如表单的上下文中非常有用。
  • 我发现这在函数视图中也很有用。有了上面的代码,我就可以从函数视图中调用基于类的视图了。
  • 不再工作了.. 到目前为止,我看到的唯一选择是使用 django.test.Client
  • 但是在使用 DRF 时会抛出 The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
相关资源
最近更新 更多