'href not accepting loop Django
<table id="tabledata" class="tablecenter" style=" position: static; top:50%;">
<tr>
<th>Username</th>
<th>Email</th>
<th>Role</th>
<th>Action</th>
<th>Remove User</th>
</tr>
{% for user in all %}
<tr>
<td>{{user.first_name}} {{user.last_name}}</td>
<td>{{user.email}}</td>
<td>{% if user.is_staff %} Admin {% else %} Practitioner {% endif %}</td>
<td><a class="openButton" onclick="openForm()">Edit</a></td>
<td><a href="{% url 'delete_user' pk=user.id %}" class="openButton">Delete</a></td>
</tr>
{% endfor %}
After running the server on local, the page gives syntax error.
Solution 1:[1]
You are mixing url and path syntax. You use of path converters with path(…) [Django-doc], so:
# app_name/urls.py
from django.urls import path
urlpatterns = [
# …,
path('delete_user/<int:pk>/', views.delete_user, name='delete_user'),
# …
]
Solution 2:[2]
I think you should use
According to docs https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#url
<table id="tabledata" class="tablecenter" style=" position: static; top:50%;">
<tr>
<th>Username</th>
<th>Email</th>
<th>Role</th>
<th>Action</th>
<th>Remove User</th>
</tr>
{% for user in all %}
<tr>
<td>{{user.first_name}} {{user.last_name}}</td>
<td>{{user.email}}</td>
<td>{% if user.is_staff %} Admin {% else %} Practitioner {% endif %}</td>
<td><a class="openButton" onclick="openForm()">Edit</a></td>
<td><a href="{% url 'delete_user' user.id %}" class="openButton">Delete</a></td>
</tr>
{% endfor %}
EDIT
In the tutorial guy is using this url
url(r'^delete_user/<str:pk>', views.delete_user, name='delete_user')
but you are using
url(r'^delete_user/<int:pk>', views.delete_user, name='delete_user')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
