Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually calling a class based generic view

Tags:

I'm currently trying to call a class based Generic view from within another class based generic view and cant seem to do it correctly.

Ways I've tried:

result = CategoryTypes.as_view()  # The same way you put it in the urlconf print result 

Prints: <function CategoryTypes at 0x92bd924>

CategoryTypes.as_view()(self.request) # & CategoryTypes().dispatch(self.request) 

Tracebacks:

ContentNotRenderedError at /crm/categories/company/ The response content must be rendered before it can be accessed.

result = CategoryTypes().__init__() print result 

Prints: None

How do I call this from another view? I've seriously tried every method on the class and way of calling it I can think of.

like image 987
Joshua Jonah Avatar asked Aug 31 '11 14:08

Joshua Jonah


People also ask

How do you pass context in class based view?

Passing context into your templates from class-based views is easy once you know what to look out for. There are two ways to do it – one involves get_context_data, the other is by modifying the extra_context variable. Let see how to use both the methods one by one.

When should you use a generic view and when shouldn't you use a generic view?

The intention of Generic Views is to reduce boilerplate code when you repeatedly use similar code in several views. You should really use it just for that. Basically, just because django allows something you are doing generically you shouldn't do it, particularly not when your code becomes not to your like.

What is generic view in Python?

Django generic views are just view functions (regular old python functions) that do things that are very common in web applications. Depending on the type of app you are building, they can save you from writing a lot of very simple views.


1 Answers

The first way -- CategoryTypes.as_view()(self.request) -- is right. The problem is that if your view returns a TemplateResponse, its render method isn't called automatically.

So if you need to access the content of the response, call render() on it first.

like image 141
Ismail Badawi Avatar answered Oct 25 '22 16:10

Ismail Badawi