Search code examples
djangodjango-templatesdjango-generic-views

How can I tell if I'm doing a create or an update with django generic views (CreateView vs UpdateView) from my template?


I'm sharing the same template for my CreateView and UpdateView using django's generic views. I want the "submit" button in my template to say "Add" when I'm using the CreateView and "Update" when I'm using the UpdateView. Is there any way in my template to distinguish which view is being used (CreateView vs UpdateView)?

I know I could use a separate template using template_name_suffix and put the common stuff in a separate include or something but just wanted to see if there was a way to do it without creating a separate template.


Solution

  • When creating a new object, object will always be None, at the moment the template is rendered. You could check for the existence of {{ object }} in your template:

    {% if object %}Update{% else %}Add{% endif %}