Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Call Class based view from another class based view

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?

like image 509
psychok7 Avatar asked Feb 19 '13 11:02

psychok7


People also ask

Can I call a view from another view in Django?

To call a view from within another view with Python Django, we can call the view function directly. to call view1 in view2 . And then we can get the response returned and do what we want with it before we return the response. We can also return the response directly in view2`.

Which is better class based view or function based view Django?

Class based views are excellent if you want to implement a fully functional CRUD operations in your Django application, and the same will take little time & effort to implement using function based views.


2 Answers

Instead of

ShowAppsView.as_view()(self.request)

I had to do this

return ShowAppsView.as_view()(self.request)
like image 77
psychok7 Avatar answered Sep 21 '22 14:09

psychok7


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.

like image 26
danodonovan Avatar answered Sep 20 '22 14:09

danodonovan