How to use context with class in CreateView in django?
Before i have:
#views.py
from django.views.generic import CreateView
from cars.models import *
Since your are creating a new instance of a Car, there is no context for get_context_data
because there is no object yet. I didn't test using Mixin to get the context from another class as suggested above, but that seems reasonable. However, if I can assume you want to use the basic CreateView
, UpdateView
and DeleteView
, then I solved this by assuming I will have no context for CreateView
. Then in my template I used an if to make the decision, such as:
In DeleteView
I include:
context['buttonword'] = 'Delete'
In UpdateView
I include:
context['buttonword'] = 'Update'
As I said, I do not set buttonword
in CreateView
. Hence, when the template logic is done, if buttonword
is assigned, the word in it shows up in the button, otherwise Save shows up on the button.