Stivi B
Stivi B

Reputation: 433

Pass parameters defined “in the buttons”(template) to CRUD-view

I have a table, where I add in the last column of every row the buttons “delete” and “edit”. I do this with the url + parameters in the href in the template (see below). I wrote a function for every href + parameter and the scripts work.

            <form method="post">
                {% csrf_token %}
                <input type="hidden" name="projekt_id" value="{{objekt.id}}" />
                <a class="btn btn-outline-secondary btn-sm" href="{% url 'check:remove_project' objekt.id %}" role="button">delete</a>
                <a class="btn btn-outline-secondary btn-sm" href="{% url 'check:edit_project' objekt.id %}" role="button">edit</a>
            </form>

Since i need such tables very often I want to handle the entire functionality (view the data/edit/delete/create) in one single view (I already have this in one template). My idea/wish is to pass the name= and value= from inside the buttons to the view. There I can distinguish for the appropriate functions - by if-statements- between edit/delete/view/create…

How can the parameters be passed from the BUTTONS in template to the view? Where is the documentation?

I wonder if there is a more elegant way to solve this? (maybe a combination of class based views?)

Upvotes: 0

Views: 80

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

You can access a button like any other field in the POST data.

<button type="submit" name="delete">Delete</button>
<button type="submit" name="edit"> /Edit</button>
if "edit" in request POST:
   ...
elif "delete" in request.POST:
    ...

Upvotes: 1

Related Questions