I was wondering if I could get some advice on how to structure my URLs (and urls.py files!) in my Django project.
I’m building a relatively standard project that contains user profiles (i.e. a “public” display of a profile) and the user’s settings.
Ideally the profile would be accessed by:
website.com/profile/<slug:username>/
website.com/profile/<slug:username>/followers/
etc.
and settings…
website.com/settings/
website.com/settings/address/
etc.
Because almost all of the models, forms etc. are present in both sets of views, it feels like it makes sense for these to be handled within the same app in my project (“accounts”, “users” etc.). However feeding profile/
and settings/
to the same app-level urls.py file seems unworkable.
But If I split them into two apps, I’d have to import everything over from the original app into the new app to supply the view.
Option 1
One workaround that came to mind was to have two differently-named urls.py files in the same app, and include them separately in the project-level urls.py file.
project-level urls.py:
urlpatterns = [
path('profile/', include('account.profile_urls')),
path('settings/', include('account.settings_urls')),
]
Is this okay? I’ve not seen it done on any of the tutorials I’ve been working through.
Option 2
I guess the other thing to do is bite the bullet and rename the URLs so both can work together in a user-friendly way. Eg.
website.com/account/<slug:username>/
website.com/account/<slug:username>/followers/
website.com/account/settings/
website.com/account/settings/address/
But I’m reluctant to use non-desireable URLs just because it helps the app structure. Also I guess a user could change their username to “settings”! (Although I guess that’s handled with a restricted list).
On one hand using the URL schema to define the app structure feels wrong, as there’s lots of code that would have to be split off just because the URL says settings/ instead of profile/. But on the other hand using the app structure to dictate the URL schema feels just as weird because it should be as user-friendly as possible.
Surely there’s a way for both to be optimised without impacting the other? Is ‘Option 1’ a done thing?
Any help or advice greatly appreciated! Thanks