Search code examples
pythonhtmldjangoupdateview

Custom button in Class based UpdateView django


Problem: My UpdateView looks the same as my CreateView. I would like to change the submit button from "Check" to "Update".

Here are my views in the views.py file:

class CapsUnifCreateView(CreateView):
    template_name = 'capsules/uniformity_form.html'
    model = models.Uniformity
    fields = (
        'caps_name',
        'mass_1_caps_empty',
        'mass_20_caps_full',
        'mass_max1',
        'mass_min1',
    )

class CapsUnifUpdateView(UpdateView):
    fields = (
        'caps_name',
        'mass_1_caps_empty',
        'mass_20_caps_full',
        'mass_max1',
        'mass_min1',
    )
    model = models.Uniformity

Note, that I do not use a separate template for the UpdateView.

Something like

{% if CreateView %} Check {% else %} Update {% endif %}

in the html file would be nice, but I don't know how to implement it.

Thanks in advance!


Solution

  • Found it! In the ModelName_form.html (html file that is linked to CreateView):

        {% if not form.instance.pk %}
          <input type="submit" class="btn btn-primary" value="Check">
        {% else %}
          <input type="submit" class="btn btn-primary" value="Update">
        {% endif %}
    

    This means, if there is no primary key in the context (during CreateView), it shows "Check", else if there is a primary key (during UpdateView), it shows "Update".

    Thanks for your efforts!