Django 2.1 - creating the correct path URL for primary keys
Solution 1:
If I understood it right, your problem is basically that when you visit basic_app/1/3/
you want to see the details for the student whose primary key is 3, but instead you see a student with primary key 1.
If that's the case, it's because in your view you're using the URL field pk
while instead you should be using cpk
. Try changing your URL pattern to look like this:
path('<int:school_pk>/<int:pk>/',views.StudentDetailView.as_view(),name='student_details'),
(If this doesn't work, please post the code for StudentDetailView
.)
In general, I would suggest using more descriptive URL fields, e.g. school_pk
, student_pk
, ... so that you can avoid making confusion.
Solution 2:
Use this instead
path("<int:pk>/",views.SchoolDetailView.as_view(),name='detail')