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
JavaScript
x
6
1
def editerEmploye(request, pk):
2
editer = Employe.objects.get(id=pk)
3
4
context = {'editer':editer}
5
return render(request, 'accounts/editer_form.html', context)
6
url.py
JavaScript
1
8
1
urlpatterns = [
2
path('', views.home),
3
path('dashboard/', views.home, name="dashboard"),
4
path('employe/', views.employe, name="employe"),
5
path('disciplinaire/', views.disciplinaire, name="disciplinaire"),
6
path('employe_form/', views.createEmploye, name="employe_form"),
7
path('editer_form/<str:pk>/', views.editerEmploye, name="editer_form"),
8
employe.html
JavaScript
1
2
1
<td><a href="{% url 'editer_form' employe.id %}">Editer </a>
2
models.py
JavaScript
1
11
11
1
class Employe(models.Model):
2
Matricule = models.CharField(max_length=10, null=False)
3
Prenom = models.CharField(max_length=40, null=True)
4
Nom = models.CharField(max_length=40, null=True)
5
Tel = models.CharField(max_length=20, null=True)
6
Adresse = models.CharField(max_length=100, null=True)
7
Courriel = models.CharField(max_length=100, null=True)
8
Horaire = models.CharField(max_length=50, null=True)
9
Date_embauche = models.CharField(max_length=100, null=True)
10
data_created = models.DateTimeField(auto_now_add=True, null=True)
11
Advertisement
Answer
The problem is here:
JavaScript
1
2
1
<td><a href="{% url 'editer_form' employe.id %}">Editer </a>
2
You don’t have employe
in the context. You need to change it to:
JavaScript
1
2
1
<td><a href="{% url 'editer_form' editer.id %}">Editer </a>
2
And you should change <str:pk>
to <int:pk>
if it is id
.