What is context_object_name in django views?

If you do not provide “context_object_name”, your view may look like this: <ul> {% for publisher in object_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul> But if you provide like {“context_object_name”: “publisher_list”}, then you can write view like: <ul> {% for publisher in publisher_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul> That means … Read more

Django Class Based View for both Create and Update

I ran into a situation where I wanted something like this. Here’s what I came up with (do note that if you’re trying to use it as an update view and it can’t find the requested object, it’ll behave as a create view rather than throwing a 404): from django.views.generic.detail import SingleObjectTemplateResponseMixin from django.views.generic.edit import … Read more

‘function’ object has no attribute ‘as_view’

In my case, the problem was that I tried to use a @decorator on the class-based view as if it was a function-based view, instead of @decorating the class correctly. EDIT: From the linked page, here is a way to apply @login_required to a class-based view: from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator @method_decorator(login_required, … Read more