ZAD-Man
ZAD-Man

Reputation: 1406

How do I return the result of a class-based view from another class-based view in Django?

When I submit a certain CreateView, I want to move on to create another object in another CreateView. However, when I try

def get_success_url(self):
    return FooView.as_view()(self.request, itempk = self.object.id)

I get

ContentNotRenderedError: The response content must be rendered before it can be accessed.

I also tried

def get_success_url(self):
    return render(FooView.as_view()(self.request, itempk = self.object.id))

which gives me

AttributeError: 'TemplateResponse' object has no attribute 'META'

I'm fairly certain I'm just going about this the wrong way, and that I've done it correctly before, but I'm stumped. What is the proper way to do this?

Upvotes: 0

Views: 101

Answers (2)

Berislav Lopac
Berislav Lopac

Reputation: 17243

Since you're defining the get_success_url, I would say that you just need something like

def get_success_url(self):
    # assuming that your FooView urlconf was named "foo_view"
    return reverse('foo_view', kwargs={'itempk':self.object.id})

Cf. https://docs.djangoproject.com/en/dev/ref/urlresolvers/

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599470

You don't want to call the view, you want to redirect the user to it. So just use the redirect function:

from django.shortcuts import redirect
...
return redirect('foo_view_name', kwargs={'itempk': self.object.id})

Upvotes: 2

Related Questions