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

How To Install & setup Python 3.8 Version on Windows 10

How to Connect Django With PostgreSql / Mysql / Oracel Database

How to Connect Firebase Using Pyrebase In Python