How can i get the data-value with what statment?

Viewed 25

How can i get the data-vale?

On html file

<form method='get' action='#'>
                        <input type="submit" data-value="1" value="Edit" name="Type" />
                        <input type="submit" data-value="1" value="Delete" name="Type" />

In django views

if request.GET.get('Type') == 'Delete':
    print (request.GET.get('Delete'))
1 Answers

You can work with the <button> tag and specify a name-value pair:

<form method='get' action='#'>
    <button name="Type" value="Edit" type="submit" data-value="1">Edit</button>
    <button name="Type" value="Delete" type="submit" data-value="1">Delete</button>
</form>

or if you want to submit the value as well, we can do this with:

<form method='get' action='#'>
    <button name="Type" value="Edit-1" type="submit">Edit</button>
    <button name="Type" value="Delete-1" type="submit">Delete</button>
</form>

In that case we can check this in the view with:

if request.GET.get('Type', '').startswith('Delete'):
    __, item = request.GET['Type'].split('-', 1)

In that case item will contain the value after the hyphen.


Note: Section 9 of the HTTP protocol specifies that requests like GET and HEAD should not have side-effects, so you should not change entities with such requests. Normally POST, PUT, PATCH, and DELETE requests are used for this. In that case you make a small <form> that will trigger a POST request, or you use some AJAX calls.

Related