Are URL namespaces optional or are they used by default by Django?

39 views
Skip to first unread message

Stodge

unread,
Dec 21, 2017, 10:38:36 AM12/21/17
to Django users
I am porting an app from Django 1.6.x to 1.10.x and I'm hitting a problem with URL namespaces. I don't specifically configure any namespaces in my URLs but Django seems to think that all my app URLs are namespaced. The documentation seems to imply that namespaces are optional and only configured if provided, but my experience suggests otherwise. Would someone be able to confirm if namespaces are optional (opt-in)? I'm really rather confused at the moment about this. Thanks

Andréas Kühne

unread,
Dec 21, 2017, 10:47:28 AM12/21/17
to django...@googlegroups.com
Hi,

Namespaces are completely optional - you don't have to use it if you don't want to. 

What is happening that makes you think otherwise? What errors are you seeing?

Regards,

Andréas

2017-12-21 16:38 GMT+01:00 Stodge <sto...@gmail.com>:
I am porting an app from Django 1.6.x to 1.10.x and I'm hitting a problem with URL namespaces. I don't specifically configure any namespaces in my URLs but Django seems to think that all my app URLs are namespaced. The documentation seems to imply that namespaces are optional and only configured if provided, but my experience suggests otherwise. Would someone be able to confirm if namespaces are optional (opt-in)? I'm really rather confused at the moment about this. Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2f722055-c058-4d06-b4e7-ca47b2511769%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stodge

unread,
Dec 21, 2017, 10:53:54 AM12/21/17
to Django users
Thanks Andreas,

I'm seeing three things;

1) Reverse URLs are failing until I specifically add the app's name as a namespace
2) The show_urls management command from the django_extensions project appears to show that all URLs have a namespace:

/license/license/edit/<license_id>/ core.decorators.WVCheckLoginPermissions() license:edit_license

3) All resource URIs in TastyPie are empty, which according to a ticket I found there seems to be caused by namespaces.

Thanks
Mike



On Thursday, 21 December 2017 10:47:28 UTC-5, Andréas Kühne wrote:
Hi,

Namespaces are completely optional - you don't have to use it if you don't want to. 

What is happening that makes you think otherwise? What errors are you seeing?

Regards,

Andréas

2017-12-21 16:38 GMT+01:00 Stodge <sto...@gmail.com>:
I am porting an app from Django 1.6.x to 1.10.x and I'm hitting a problem with URL namespaces. I don't specifically configure any namespaces in my URLs but Django seems to think that all my app URLs are namespaced. The documentation seems to imply that namespaces are optional and only configured if provided, but my experience suggests otherwise. Would someone be able to confirm if namespaces are optional (opt-in)? I'm really rather confused at the moment about this. Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Stodge

unread,
Dec 21, 2017, 11:02:28 AM12/21/17
to Django users
I am using a prefix when I include the URLs for each app:

for app in settings.SITE_APPS.get_application_list():    
    url_prefix
= r'^%s/' % app
        urlpatterns
+= [
            url
(prefix, include(url_include)),

       
]

I just assumed this isn't using namespaces because I'm not using the namespace parameter on include()?

Andréas Kühne

unread,
Dec 21, 2017, 11:38:42 AM12/21/17
to django...@googlegroups.com
Ok,

You have not namespaced anything, but you have added the api name infront of the URLs you are adding.

So for example, I am guessing that you in your urls.py file for the license app have added "/license/edit/<license_id>/" for the license detail page? Because you have already added the license part with the prefix you have added here:

for app in settings.SITE_APPS.get_application_list():    
    url_prefix 
= r'^%s/' % app
        urlpatterns 
+= [
            url
(prefix, include(url_include)),

        
]


So you have added license twice, and therefore you will need to add it twice when you call the urls as well.

You should probably do this instead:
for app in settings.SITE_APPS.get_application_list():    
    url_prefix 
= r'^%s/' % app
        urlpatterns 
+= [

            url
('', include(url_include)),

        
]



If you don't want that extra license part.

Namespace is added the way you suggested via:
url('', include(url_include, namespace="license"))

Also, it doesn't result in an extra app part (in this case license), it only adds the namespace in the call to reserve, so if you had setup the namespace like i wrote here, you would use: 
reverse("license:license_detail", args=(1, ))

Hope I have explained it good enough :-)

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Stodge

unread,
Dec 21, 2017, 11:58:51 AM12/21/17
to Django users
Thanks,

My edit license URL is:

    url(r'^license/edit/(?P<license_id>\d+)/$', views.edit_license, name='edit_license'),

However, this is what I want and it has been this way for years. My URLs are organised:

/<app_name>/<resource_name>/

I don't think this explains why my URLs are all namespaced when I haven't changed my code and my code doesn't use namespaces. I'm really confused. :)

Thanks again
Mike



To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Andréas Kühne

unread,
Dec 21, 2017, 1:32:48 PM12/21/17
to django...@googlegroups.com
Hi again,

I don't understand what you mean then. Because you say that they are namespaced. What I mean by that is that when using reverse, you have to use: 
reverse("license:edit_license", args=(1, )). 

However, if this is the code you have used:
for app in settings.SITE_APPS.get_application_list():    
    url_prefix 
= r'^%s/' % app
        urlpatterns 
+= [
            url
(prefix, include(url_include)),

        
]

Then that won't work either - because you add the prefix variable to the urlpattern and not the url_prefix variable. So this would add it the same way I wanted to add it:

url('', include(url_include))

So I would fix that first at least.

Regards,

Andréas

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Stodge

unread,
Jan 10, 2018, 10:05:21 PM1/10/18
to Django users
Problem solved and apparently it was my silly mistake! While experimenting I added app_name to my urls files. Apparently this enables namespace support. So I removed them from my urls files and it works.

Thanks


On Thursday, 21 December 2017 10:38:36 UTC-5, Stodge wrote:
Reply all
Reply to author
Forward
0 new messages