Opened 13 years ago

Closed 10 years ago

Last modified 10 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: dev
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')),

IT DOES WORK !

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/__init__.py
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 by Aymeric Augustin, 13 years ago

Description: modified (diff)

Fixed formatting (please use preview).

comment:2 by Aymeric Augustin, 13 years ago

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 by Malcolm Tredinnick, 12 years ago

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 by k.znamenskiy@…, 12 years ago

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 by Amirouche, 12 years ago

Cc: amirouche.boubekki@… added

comment:6 by Amirouche, 12 years ago

Cc: amirouche.boubekki@… removed

comment:7 by Florian Apolloner, 10 years ago

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

The behaviour is as documented in: https://docs.djangoproject.com/en/1.7/topics/http/urls/#url-namespaces -- 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 by Tim Graham, 10 years ago

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

comment:9 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 27eeb64a96ec6d3a05714ab6319b500a935706fb:

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

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

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