Opened 3 years ago

Closed 7 weeks ago

Last modified 7 weeks ago

#17716 closed Cleanup/optimization (fixed)

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

Reported by: blackip@… Owned by: timgraham
Component: Core (URLs) Version: master
Severity: Normal Keywords: include reverse url
Cc: apollo13 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 aaugustin)

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 Changed 3 years ago by aaugustin

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Fixed formatting (please use preview).

comment:2 Changed 3 years ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

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 3 years ago by mtredinnick

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 2 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 2 years ago by abki

  • Cc amirouche.boubekki@… added

comment:6 Changed 2 years ago by abki

  • Cc amirouche.boubekki@… removed

comment:7 Changed 3 months ago by apollo13

  • Cc apollo13 added
  • Version changed from 1.4-alpha-1 to master

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 Changed 7 weeks ago by timgraham

  • Has patch set
  • Owner changed from nobody to timgraham
  • Status changed from new to assigned
  • Type changed from Bug to Cleanup/optimization

comment:9 Changed 7 weeks ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 27eeb64a96ec6d3a05714ab6319b500a935706fb:

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

comment:10 Changed 7 weeks 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