Django Call Class based view from another class based view

前端 未结 2 1721
醉梦人生
醉梦人生 2021-02-03 23:16

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 ShowApps         


        
相关标签:
2条回答
  • 2021-02-03 23:28

    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.

    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;

    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.

    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.

    * 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.

    0 讨论(0)
  • 2021-02-03 23:43

    Instead of

    ShowAppsView.as_view()(self.request)
    

    I had to do this

    return ShowAppsView.as_view()(self.request)
    
    0 讨论(0)
提交回复
热议问题