I've already partially solved an issue I was struggling with but I would like some clarity on if this is the right way to do it in the first place.
Ultimately, I wanted to my URL's to follow something like this: www.whatever.com/company/{company_id}/person/{patient_id} (i.e. whatever.com/company/4/patient/2)
That's not actually perfect because what I really want is the name of the company and patient where the ID is, so www.whatever.com/company/ryan-corp/patient/jim-jones. I'll leave that for another day for now.
What I ended up doing to get the result with the ID is this:
my_project/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('company/', include('scraper.urls', namespace="company")),
]
my_app/urls.py
app_name = "scraper"
urlpatterns = [
path('', company),
path('<int:pk>/', company_detail),
path('<int:company>/patient/<int:pk>/', patient_detail),
]
my_app/views.py
def patient_detail(request, pk, company):
patient = Patient.objects.get(id=pk)
return render(request, 'scraper/patient_detail.html', {'patient':patient})
This doesn't feel like I am following a best practice and I feel like I literally guessed to get this working by reading the documentation found here: https://docs.djangoproject.com/en/3.1/topics/http/urls/
It esentially laid out how Django processes the URL's and I noticed that it basically grabs each part of the URL as kwargss. Which is why I passed the company variable in the patient_detail function in views. Then I took a guess and added <int:company>
and it worked.
What is the best way to actually achieve this result in a Django approved method? Again, it works but I feel like this is a hack and there is a better way.