Sameer Zahid
Sameer Zahid

Reputation: 611

Django - How to create a simple confirmation view?

I'm trying to create a View in which the user is asked to confirm an action of updating a value of an instance. I want the View to only have a submit button. What I want is similar to a DeleteView except what I want is to update a value of a record instead of deleting the record.

I have tried using Class based UpdateView, but it requires that I specify a fields parameter in which I must specify at least one field and I do not want to show any field, just the submit button.

This is what my template now looks like:

<h4>Complete {{ item.name }}?</h4>

<form method='POST' class="gia-form">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" class="button" value="Complete Item">
    <a href="{% url 'items:home' %}">Cancel</a>
</form>

Url for the view:

url(r'^action/complete-item/(?P<pk>\d+)/$', views.CompleteItemView.as_view(), name='complete'),

My UpdateView:

class CompleteItemView(UpdateView):
    model = models.Item
    template_name = 'items/complete_item.html'
    fields = ("status",)

    def form_valid(self, form):
        form.instance.status = 'completed'
        return super().form_valid(form)

Above I have chosen to show the status field, and this is what I'm trying to get rid of in an elegant way as I just want to show the confirm button without any fields.

Upvotes: 2

Views: 6734

Answers (3)

Danielius
Danielius

Reputation: 165

You could use Javascript like this:

<script>
$(document).on('click', '.some_class', function(){
return confirm('Are you sure you want to update this?');
})
</script>

And add class some_class to your button

Upvotes: 0

drew
drew

Reputation: 781

Have you considered using Bootstrap Modals.

You could have a 'Submit' button which wouldn't actually submit the form, but instead it would open the modal where you could have the form submit button.

You would just have to remember to create the modal inside the form tags.

Upvotes: 0

Selcuk
Selcuk

Reputation: 59228

Instead of

{{ form.as_p }}

You can simply write

<input name="{{ form.status.html_name }}" id="{{ form.status.id_for_label }}"
       value="{{ form.status.value }}" type="hidden">

This will render the status field as hidden and only the Submit button will be visible.

Upvotes: 1

Related Questions