[Django]-Referencing multiple submit buttons in django

40đź‘Ť

âś…

Give the input types a name and look for them in your request.POST dictionary.

E.g.:

<form action="/" method="post">
    {% csrf_token %}

    <input type="text" name="{{ email.id }}" value=" {{email}}"></td>
    <td><input type="submit" value="Edit" name="_edit"></td>
    <td><input type="submit" value="Delete" name="_delete"></td>
</tr>

and in views.py something like

if request.POST:
    if '_edit' in request.POST:
         do_edit()
    elif '_delete' in request.POST:
         do_delete()

EDIT: Changed d.has_key(k) to k in d per Daniel’s comment. has_key is deprecated in python 3.0 and the in style is preferred as its more generic — specifically d.has_key(k) fails if d isn’t a dictionary, but k in d works for any d that’s an iterable (e.g., dict, string, tuple, list, set).

👤dr jimbob

10đź‘Ť

I know this question was asked a long time ago, but for the record here is a solution using class-based views. It uses the same html as in dr jimbob’s answer.

from django.views.generic.edit import FormView

class MyView(FormView):
    template_name = 'mytemplate.html'
    form_class = MyForm

    def form_valid(self, form):
        if '_delete' in self.request.POST:
            # do delete
        elif '_edit' in self.request.POST:
            # do edit

Note the default behavior of form_valid is:

return HttpResponseRedirect(self.get_success_url())

5đź‘Ť

You would need to give the submit buttons a name attribute, say “action” for example, then you could reference them in the request.POST collection:

def my_view(request):
    action = request.POST.get('action')
    if action == 'Edit':
        #do edit
    else:
        # do delete

Hope that helps you out.

0đź‘Ť

<form action="/" method="post">
<tr>
    <td><input type="text" name="{{ email.id }}" value=" {{email}}"></td>
    <td><input type="submit" value="Edit" name="submit"> </td>
    <td><input type="submit" value="Delete" name="submit"> </td>
</tr>
</form>

views.py will be –

def our_view(request):
    if request.POST.get('submit') == 'Edit':
        make_edit()
    elif request.POST.get('submit') == 'Delete':
        make_deletes()
    else:
        raise Http404()
👤sameer

0đź‘Ť

Here is a solution using class based views: use this mixin in both your CreateView and UpdateView

class CreateOrUpdateViewMixin:

    def form_valid(self, form):

        instance = form.save(commit=False)
        if "_save_as_new" in self.request.POST:
            instance.pk = None
        # Next row is optional
        instance.slug = instance._generate_unique_slug 
        self.object = instance
        return super().form_valid(form)

And write your form as follows:

<form
    action="{{ instance.get_update_url }}"
    method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button
        type="submit">
        {% trans "Update the instance" %}
    </button>
    <button
        type="submit"
        name="_save_as_new">
        {% trans "Save as new" %}
    </button>
</form>

Credits to @Rushing_tadpole for suggesting using class based views.

Leave a comment