How To Pass Values From Html To Django Through URL

This can be done in three simple steps:
1) Add item id with url tag:
{% for item in post %}
<tr>
  <th>{{ item.id }}</th>
  <td>{{ item.title }}</td>
  <td>{{ item.body }}</td>
  <td>
    <a href={% url 'edit' id=item.id %}>Edit</a>
    <a href={% url 'delete' id=item.id %}>Delete</a>
  </td>
</tr>
{% endfor %}
2) Add path to urls.py:
path('edit\\/(?P<id>[0-9]+)$', views.edit, name='edit')path('delete\\/(?P<id>[0-9]+)$', views.delete, name='delete')
3) Use the id on views.py:
def delete(request, id):
    obj = post.objects.get(id=id)
    obj.delete()

    return redirect('dashboard')

Html Nunchi Django Application ki url lo value ni ele pass Chayali and Ele update chaye 


Comments

Popular posts from this blog

ERR: error parsing query: found influx, expected SELECT, DELETE, SHOW, CREATE, DROP, EXPLAIN, GRANT, REVOKE, ALTER, SET, KILL at line

How to get Full URL or get absolute url In Django

How To Convert Html File/URL/String Into Image Using Python