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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With