#Multi-app shared url namespace

41 messages · Page 1 of 1 (latest)

pale carbon
#

How can I share a URL namespace between two apps?

E.g. I have /api/, and I have two apps: app1 and app2.

Let's say I want /api/app1topic/abc/, I want that to be api:app1topic:abc

The issue I think i'm having is that I have it structured

  • app2/app2topicABC/api/urls.py
  • app2/app2topicXYZ/api/urls.py

rather than a central app2/api/urls.py

But it should definitely still be possible, cause a namespace should have nothing to do with the file structure, at least I thought.

#

Multi-app shared url namespace

#

When I tried with


path("", include("app2.urls")),
path("api/app1topic/", include("app1.app1topic.api.urls")),

with app_name = "app1topic" in app1topic>api>urls.py it didn't work 'app1topic' is not a registered namespace inside 'api'

Not too sure, I feel like i'm missing an argument for the core urls to force a namespace

patent knot
#

I'm not getting from example what you want to get

pale carbon
#

To be able to use api: namespace, but via multiple (reusable)apps

patent knot
#

well, have you tried stating it explicitly?

pale carbon
#

The fake names made it more confusing.

Lets say we have:

  • api/admin/create_user/
  • api/books/covers/create_cover/

So we have two apps

  • core (reusable, imported via package)
  • books

In my main books/urls.py i have


path("", include("core.urls")),
path("api/books/covers/", include("books.covers.api.urls"))

I can use api:admin:create_user but I can't use api:books:covers:create_cover cause I get 'books' is not a registered namespace inside 'api'

#

And I tried even with

# books/urls.py (main)

path("api/", include("books.api.urls")),

with

# books/api/urls.py

path("books/covers/", include("books.covers.api.urls")),

app_name = "api"

But still same error

patent knot
#

so, did you try include("someurls", namespace="api") ?

pale carbon
#

I tried a few different syntaxes with the namepace arg, none seemed to do anything so im not sure if I was putting in the wrong place. I'll try now

#

Nope still the same error. I think the app_name overrides it anyways, but it's the same value in namespace arg and appname

patent knot
#

Nope, I'm pretty sure namespace overrides app_name

pale carbon
#

Well either way it's the same value so nothing changes

patent knot
#

I'm still confused with structure though

pale carbon
#

Yeah at some point I moved everything from

/api/topic1/abc/ to /topic1/api/abc/ for everything and it didnt affect anything until now

patent knot
#

e.g. why do you have api:books:covers namespace?

#

your are actually nesting urls.py like that?

pale carbon
#

Yeah so I have e.g.

  • app/books/urls.py
  • app/books/covers/urls.py
  • app/books/covers/api/urls.py
#

Tho ive confused it more with a random name again.

In reality it's

app/finance/api/reports/xyz

So the URL is /api/finance/reports/xyz/

patent knot
#

but so if you include("books.covers.api.urls", namespace="api")
reference should be api:create_cover not api:books:covers:create_cover

pale carbon
#

Yeah good shout. I just tried to create a blank /api folder with just urls.py but that didnt work either

patent knot
#

later would be in case where you consequtively nest urls.py and include lower one

pale carbon
#
path("api/", include("backend.api.urls", namespace="api")),
# /app/api/urls.py

urlpatterns = [
    path("finance/", include("app.finance.api.urls", namespace="finance")),
]

app_name = "api"

# /app/finance/api/urls.py

urlpatterns = [
    path("reports/", include("app.finance.api.reports.urls", namespace="reports")),
]

app_name = "finance"

'finance' is not a registered namespace inside 'api'

patent knot
#

When the error happens? Just from setting namespace or when trying reversing a pattern?

pale carbon
#

when trying to actually resolve

#

when just starting without visiting there is a warning:?: (urls.W005) URL namespace 'api' isn't unique. You may not be able to reverse all URLs in this namespace

#

I didn't see that before

patent knot
#

hm, I wonder if that warning is actual problem or just notice that it can have clashes potentially

#

what reverse argument you are trying?

pale carbon
#

Inside of the template it's trying {% url 'api:finance:invoices:single:fetch' %}

patent knot
#

hm, that's strange finance looks here like direct nesting, I'm not sure why it wouldn't work

pale carbon
#

Yeah it's quite odd

patent knot
#

try without app_name ? Honestly i always used namespace through argument, it's first how I learned and it was cleaner to me

#

I think I had a project with complex includes too, but I don't remember well how it was structured

pale carbon
#

Still the same result without both namespace args

#

But yeah I worry too much about the structure, then end up restructuring the whole thing, then by the end of it i've wasted 5 hours and got no further than before

pale carbon
#

I fixed it! You were right, it was the namespace args. I added namespace="API" in the main urls.py AND the core one. I didn't think of using it in the dependant app but that seems to have fixed it. Or just logging in another day fixed it.. we'll never know

pale carbon
#

❌ can confirm it did not fix it.. no idea what I thought I did the other day but it doesn't work

pale carbon
#

Just realised there's a much easier approach.. just adding a namespace for the core app. So just means an extra prefix but it resolves the errors

patent knot
#

So app urls included into core urls? Then it makes sense.

pale carbon