I am back with more django questions on CBVs. This is about context_object_name. I have the following:

@method_decorator(verified_email_required, name='dispatch')
class Create(CreateView):
    model = Profile
    context_object_name = 'profileForm'
    template_name = 'Members/template_includes/profile/form.html'
    form_class = ProfileForm
    success_url = '/Members'
    form_title = "New Login Profile Information"

    def get(self, request, *args, **kwargs):
            return render(request, self.template_name, {
                'profileTitle': self.form_title,
            })

I am using PyCharm and can put a breakpoint in the template_name form and see what the environment knows about. I expect to see a dict named profileForm with all the form members in it plus profileTitle. Instead I see profileTitle as a standalone member. I do not see anything named profileForm or object_list and the expected form members are not being painted in the template.

I suppose that I understand that the extra content in the return render will pass a "naked" profileTitle but I did expect that the default get behaviour would pull in the form info.

Have I missed the point?

1 Answers

1
Joseph On Best Solutions

You've overridden the get method in your CreateView-subclass and in doing so, you've bypassed the included functionality that a CreateView does to fill your context. If you take a look here you can see that a CreateView would otherwise call return self.render_to_response(self.get_context_data()) (because it inherits from ProcessFormView) and it's within get_context_data() (ref) that those included context variables are set up.