Django contect_object_name is not producing a contect dict in view
Django contect_object_name is not producing a contect dict in view
I am back with more django questions on CBVs. This is about context_object_name
. I have the following:
context_object_name
@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.
template_name
profileForm
profileTitle
profileTitle
profileForm
object_list
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.
return render
profileTitle
get
Have I missed the point?
1 Answer
1
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.
get
CreateView
CreateView
CreateView
return self.render_to_response(self.get_context_data())
get_context_data()
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
Ah! thank you. over-riding methods bites me often.
– 7 Reeds
Sep 17 '18 at 1:23