I have a problem with my code I’m trying to create an edit button to modify the employees subsequently displayed and make the changes but I can’t display with dynamic url in django.
views.py
def editerEmploye(request, pk): editer = Employe.objects.get(id=pk) context = {'editer':editer} return render(request, 'accounts/editer_form.html', context)
url.py
urlpatterns = [ path('', views.home), path('dashboard/', views.home, name="dashboard"), path('employe/', views.employe, name="employe"), path('disciplinaire/', views.disciplinaire, name="disciplinaire"), path('employe_form/', views.createEmploye, name="employe_form"), path('editer_form/<str:pk>/', views.editerEmploye, name="editer_form"),
employe.html
<td><a href="{% url 'editer_form' employe.id %}">Editer </a>
models.py
class Employe(models.Model): Matricule = models.CharField(max_length=10, null=False) Prenom = models.CharField(max_length=40, null=True) Nom = models.CharField(max_length=40, null=True) Tel = models.CharField(max_length=20, null=True) Adresse = models.CharField(max_length=100, null=True) Courriel = models.CharField(max_length=100, null=True) Horaire = models.CharField(max_length=50, null=True) Date_embauche = models.CharField(max_length=100, null=True) data_created = models.DateTimeField(auto_now_add=True, null=True)
Advertisement
Answer
The problem is here:
<td><a href="{% url 'editer_form' employe.id %}">Editer </a>
You don’t have employe
in the context. You need to change it to:
<td><a href="{% url 'editer_form' editer.id %}">Editer </a>
And you should change <str:pk>
to <int:pk>
if it is id
.