Django从另一个基于类的视图调用基于类的视图

时间:2022-02-15 12:39:35

i am trying to call a class based view and i am able to do it, but for some reason i am not getting the context of the new class that i am calling

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

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)

What am i doing wrong guys?

我做错了什么人?

2 个解决方案

#1


40  

Instead of

代替

ShowAppsView.as_view()(self.request)

I had to do this

我不得不这样做

return ShowAppsView.as_view()(self.request)

#2


1  

Things get more complicated when you start using multiple inheritance in python so you could easily be trampling your context with that from an inherited mixin.

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

You don't quite say which context you are getting and which one you want (you're not defining a new context), so it's difficult to completely diagnose, but try rearranging the order of your mixins;

你不太清楚你得到的上下文和你想要的上下文(你没有定义新的上下文),所以很难完全诊断,但是要尝试重新排列mixin的顺序;

class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView):

this implies that LoginRequiredMixin will be the first class to inherit from, and so it will take precedence over the others if it has the attribute you're looking for - if it hasn't then python will look in CurrentUserIdMixin and so on.

这意味着LoginRequiredMixin将是第一个继承的类,因此如果它具有您正在寻找的属性,它将优先于其他类 - 如果它没有,那么python将在CurrentUserIdMixin中查找,依此类推。

If you want to be really sure that you get the context that you're after, you could add an override like

如果你想确定你得到了你所追求的上下文,你可以添加一个覆盖,如

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

to ensure that the context you get is the one from the mixin that you want.

确保你得到的上下文是你想要的mixin。

* Edit * I don't know where you've found compute_context but it's not a django attribute so will only get called from ShowAppsView.get() and never in ManageAppView.

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

#1


40  

Instead of

代替

ShowAppsView.as_view()(self.request)

I had to do this

我不得不这样做

return ShowAppsView.as_view()(self.request)

#2


1  

Things get more complicated when you start using multiple inheritance in python so you could easily be trampling your context with that from an inherited mixin.

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

You don't quite say which context you are getting and which one you want (you're not defining a new context), so it's difficult to completely diagnose, but try rearranging the order of your mixins;

你不太清楚你得到的上下文和你想要的上下文(你没有定义新的上下文),所以很难完全诊断,但是要尝试重新排列mixin的顺序;

class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView):

this implies that LoginRequiredMixin will be the first class to inherit from, and so it will take precedence over the others if it has the attribute you're looking for - if it hasn't then python will look in CurrentUserIdMixin and so on.

这意味着LoginRequiredMixin将是第一个继承的类,因此如果它具有您正在寻找的属性,它将优先于其他类 - 如果它没有,那么python将在CurrentUserIdMixin中查找,依此类推。

If you want to be really sure that you get the context that you're after, you could add an override like

如果你想确定你得到了你所追求的上下文,你可以添加一个覆盖,如

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

to ensure that the context you get is the one from the mixin that you want.

确保你得到的上下文是你想要的mixin。

* Edit * I don't know where you've found compute_context but it's not a django attribute so will only get called from ShowAppsView.get() and never in ManageAppView.

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