Opened 6 years ago

Closed 3 years ago

Last modified 3 years ago

#17716 closed Cleanup/optimization (fixed)

include(arg, namespace=None, app_name=None) replace app_name with namespace

Reported by: blackip@… Owned by: Tim Graham
Component: Core (URLs) Version: master
Severity: Normal Keywords: include reverse url
Cc: Florian Apolloner Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Aymeric Augustin)

When we register urls - we can specify app_name like this

urlpatterns = patterns('',
    url(r'^inventory/', include('inventory.urls', app_name='inventory')),
    url(r'^cart/', include('cart.urls',app_name='cart')),

Then we can use reverse method or {% url cart:cart %} to generate url.

But it does not work

got NoReverseMatch at /cart/

u'cart' is not a registered namespace
Request Method:	GET
Request URL:	http://localhost:8000/cart/
Django Version:	1.4a1

When I register urls like this

    url(r'^inventory/', include('inventory.urls', namespace='inventory')),
    url(r'^cart/', include('cart.urls',namespace='cart')),


According to documentation format for urls should be

{% url app_name:view_name %}

but actual format is

{% url namespace:view_name %}

I think this is a bug

The source of bug may be in
file /usr/local/lib/python2.7/site-packages/django/conf/urls/
function include(arg, namespace=None, app_name=None)

It returns tuple (urlconf_module, app_name, namespace)

It looks like it should be (urlconf_module, namespace, app_name)

Change History (10)

comment:1 Changed 6 years ago by Aymeric Augustin

Description: modified (diff)

Fixed formatting (please use preview).

comment:2 Changed 6 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted

Reading the URL namespaces docs, it appears that it's invalid to write an include with app_name but without namespace.

Django could check that app_name and namespace are either both None, or both not None, in django.conf.urls.url, making this error is easier to debug. Accepting on this basis.

comment:3 Changed 6 years ago by Malcolm Tredinnick

We should either make this an error, or make it work. Aymeric has a cunning plan that requires this to work in some sense, so we'll probably go for the latter approach (might not be too hard).

In any case, the ambiguous and non-documented, confusing behaviour is currently unacceptable.

comment:4 Changed 5 years ago by k.znamenskiy@…

I'm new in django and begin learning info about urls, app_name and namspace

It's realized so intricately:
when include

url(r'^anypages1/', include('anypages.urls', app_name='anypages' namespace='anypages1'))
url(r'^anypages2/', include('anypages.urls', app_name='anypages' namespace='anypages2'))

when reverse

reverse('anypages:url_name', args=[myarg], current_app='anypages1') 
reverse('anypages:url_name', args=[myarg], current_app='anypages2') 

now app_name used in viewname as namespace and namespace pass as curreny_app parameter
so so intricately!

may be at least rename parameter current_app to current_namespace? Or any another actions for more consistency...

Sorry for my english:)

comment:5 Changed 5 years ago by Amirouche

Cc: amirouche.boubekki@… added

comment:6 Changed 5 years ago by Amirouche

Cc: amirouche.boubekki@… removed

comment:7 Changed 3 years ago by Florian Apolloner

Cc: Florian Apolloner added
Version: 1.4-alpha-1master

The behaviour is as documented in: -- we might wanna raise an error though if app_name is used without a namespace.

To explain why this behaviour is as documented:

{% url app_name:view_name %}
does not work, cause in step 4 of the referenced docs, there is no application instance (you didn't specify namespace)

{% url namespace:view_name %}
does work, cause it finds it as instance namespace in step 5.

To fix the broken example above use:

url(r'^cart/', include('cart.urls',app_name='cart',namespace='cart')), -- This way reverse will terminate at step 3.

comment:8 Changed 3 years ago by Tim Graham

Has patch: set
Owner: changed from nobody to Tim Graham
Status: newassigned
Type: BugCleanup/optimization

comment:9 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 27eeb64a96ec6d3a05714ab6319b500a935706fb:

Fixed #17716 -- Prevented include('...', app_name='...') without a namespace.

comment:10 Changed 3 years ago by Tim Graham <timograham@…>

In 7b5aa20a281d1df27a9d94dd591845838a3cc804:

[1.8.x] Fixed #17716 -- Prevented include('...', app_name='...') without a namespace.

Backport of 27eeb64a96ec6d3a05714ab6319b500a935706fb from master

Note: See TracTickets for help on using tickets.
Back to Top