从另一个基于类视图Django的呼叫类基础观点(Django Call Class based vi

2019-08-17 01:32发布

我试图调用基于类视图,我能够做到这一点,但由于某种原因,我没有得到的新一类我打电话上下文

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)

我在做什么错了人?

Answer 1:

代替

ShowAppsView.as_view()(self.request)

我不得不这样做

return ShowAppsView.as_view()(self.request)


Answer 2:

事情就当你开始使用更复杂的多重继承在Python,所以你可以很容易地从一个继承混入践踏与您的上下文。

你也没有说你正在和你想要的(你不定义一个新的上下文)其中一个方面,所以现在还很难完全诊断,但尝试重新安排你的混入的顺序;

class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView):

这意味着LoginRequiredMixin将要继承一流的,所以它会优先于其他人,如果有你要找的属性-如果它不是那么Python会看在CurrentUserIdMixin等。

如果你想成为真正确保你得到你后的背景下,您可以添加喜欢一个覆盖

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

以确保您获得上下文是你想要的混入之一。

*编辑*我不知道你发现compute_context ,但它不是一个Django的属性,这样将只能从所谓ShowAppsView.get()决不ManageAppView



文章来源: Django Call Class based view from another class based view