0

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.

Guy
  • 79
  • 7
  • you want name instead of id in url only way is to add slug field in model . you also refer to [q&a](https://stackoverflow.com/questions/427102/what-is-a-slug-in-django) for more details about slug field . – Meet Mar 19 '21 at 06:17

1 Answers1

0

The general "best-practice" Django URL structure is pretty accurate to what you have, in terms of using path() or url(). One recommendation I will make is that you add a namespace for the route so you can call it in your views or templates, like so:

# urls.py

from django.urls import path
from .views import *

urlpatterns = [
    path('home', home, name='homeview'),
]

# no need for views.home here since all views are imported on line 2

To use the route in a template, you'd do it like so:

<a href="{% url 'homeview' %}">Home</a>

I believe the regex url() function is decarecated since v3.1 so I won't show an example of it but it can be found here.

vladthelad
  • 165
  • 6
  • 1
    So is int:company i.e. int:object_ID or what? What would be the way to do it where the URL was something like company name instead of the ID? – Guy Mar 19 '21 at 18:06
  • you can easily put anything through as a string, no need to always have an integer. A string would be passed in the urls like so: `path('detail//', ...)` and in views you'd get it the same way as integer, `def view(request, company): ...` – vladthelad Mar 19 '21 at 20:59