Call Class based view from another class based view

36 views
Skip to first unread message

psychok7

unread,
Feb 19, 2013, 6:34:42 AM2/19/13
to django...@googlegroups.com
hi, 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

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

def compute_context(self, request, username):
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 {'totalMiles': totalMiles , 'cities_list':cities_list,'rating_list':rating_list,'allcities' : allcities, 'allcategories':allcategories}

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

def get(self, request, username, **kwargs):
return self.render_to_response(self.compute_context(request,username))

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

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

What am i doing wrong guys?

Tom Evans

unread,
Feb 19, 2013, 7:08:53 AM2/19/13
to django...@googlegroups.com
You're not returning the response created by calling the ShowAppsView
CBV, you just discard it. After that, control flows back in to the
ManageAppView CBV.

Cheers

Tom

psychok7

unread,
Feb 19, 2013, 7:14:57 AM2/19/13
to django...@googlegroups.com, teva...@googlemail.com
What do you mean not returnig?? i have a return statement. how should i do it?

Jirka Vejrazka

unread,
Feb 19, 2013, 7:26:35 AM2/19/13
to Django users, teva...@googlemail.com
You have

ShowAppsView.as_view()(self.request)

at the end of the code you pasted below. That means that you get result of the ShowAppsView which gets immediatelly discarded ("forgotten") because you don't do anything with it.

You probably want

return ShowAppsView.as_view()(self.request)

but that's just a wild guess, I have not read your code thoroughly nor I use CBV's myself.

  HTH

    Jirka

psychok7

unread,
Feb 19, 2013, 7:30:49 AM2/19/13
to django...@googlegroups.com, teva...@googlemail.com
Thats it, it now works. thanks a bunch

Tom Evans

unread,
Feb 19, 2013, 7:33:04 AM2/19/13
to psychok7, django...@googlegroups.com
On Tue, Feb 19, 2013 at 12:14 PM, psychok7 <nun...@gmail.com> wrote:
> What do you mean not returnig?? i have a return statement. how should i do
> it?
>

You have this code:

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

When you call the other CBV like this, it produces a response. You
don't store it anywhere, so it is discarded. In effect, you run the
entire other view, produce a response and throw it away.

I thought you should return the response at this point, but on closer
inspection of the code, this is in compute_context(), which probably
is expecting a context to be returned, not a response.

You may need someone with more experience in CBVs.

Cheers

Tom

PS: Please don't email me directly in reply, I replied to the original
list email, it's quite likely I'll read any follow ups posted to list,
I don't need them in my inbox as well.
Reply all
Reply to author
Forward
0 new messages