#17716 closed Cleanup/optimization (fixed)
include(arg, namespace=None, app_name=None) replace app_name with namespace
Reported by: | 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 )
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 , 13 years ago
Description: | modified (diff) |
---|
comment:2 by , 13 years ago
Triage Stage: | Unreviewed → 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 by , 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 , 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 , 12 years ago
Cc: | added |
---|
comment:6 by , 12 years ago
Cc: | removed |
---|
comment:7 by , 10 years ago
Cc: | added |
---|---|
Version: | 1.4-alpha-1 → 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 by , 10 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
Type: | Bug → Cleanup/optimization |
comment:9 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Fixed formatting (please use preview).