I have two django apps with URLs
app_name = 'app1'
urlpatterns = [
path('url1/', ..., name='name1')
path('<slug:username>/', ..., name='name2')
]
and
app_name = 'app2'
urlpatterns = [
path('url2/', ..., name='name3')
path('<slug:username>/action2/', ..., name='name4')
]
This would not work if I include them in the master urlpatterns
as
urlpatterns = [
path('', include('app1.urls'),
path('', include('app2.urls'),
]
because url2/
would first match <slug:username>/
and trigger an error for unknown username.
There are a few potential solutions but none works very well for me:
Use non-slug
url2
such as~url2
. That means allurls
inapp2
has to start with something like~
or^
.Redefine some URLs in the master
urlpatterns
but then I will have to importviews
from the apps and remove urls from the appurlpattern
.Use regular expression to explicitly exclude some names from the
<slug:username>
. This could work but then any changes inapp2
urlpatterns need to be reflected inapp1
's<slug:username> ... exclude certain names
.
It is possible to do something like
urlpatterns = [
path('', include('app1.urls'), # non-user part
path('', include('app2.urls'), # non-user part
path('', include('app1.urls'), # user part
path('', include('app2.urls'), # user part
]
so that fixed-name URLs will be matched before <slug:username>
?