Home > Software design >  href not accepting loop Django
href not accepting loop Django

Time:05-07

<table id="tabledata"  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  onclick="openForm()">Edit</a></td>
    <td><a href="{% url 'delete_user' pk=user.id %}" >Delete</a></td>
</tr>
{% endfor %}
  

After running the server on local, the page gives syntax error.

enter image description here

CodePudding user response:

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'),
    # …
]

CodePudding user response:

I think you should use

According to docs https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#url

<table id="tabledata"  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  onclick="openForm()">Edit</a></td>
    <td><a href="{% url 'delete_user' user.id %}" >Delete</a></td>
</tr>
{% endfor %}
  • Related