0

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!

Amun_Re
  • 136
  • 6

2 Answers2

0

Use get context data in your view

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',
)
def get_context_data(self, **kwargs):
    context = super(CapsUnifCreateView, self).get_context_data(**kwargs)
    context['is_update'] = False
    return context

class CapsUnifUpdateView(UpdateView):
    fields = (
        'caps_name',
        'mass_1_caps_empty',
        'mass_20_caps_full',
        'mass_max1',
        'mass_min1',
    )
    model = models.Uniformity
    def get_context_data(self, **kwargs):
        context = super(CapsUnifUpdateView, self).get_context_data(**kwargs)
        context['is_update'] = True
        return context

In your template check

{% if is_update %} Update {% else %} Create {% endif %}
Akram
  • 958
  • 5
  • 11
  • Thanks for your answer Akram. I think it is easier to use the method I posted. It only needs adaptation on the frontend in the html file. – Amun_Re Jan 28 '22 at 10:01
0

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!

Amun_Re
  • 136
  • 6