#21899 closed Cleanup/optimization (duplicate)
Reverse does not work with function and namespace
Reported by: | Artem Skoretskiy | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.4 |
Severity: | Normal | Keywords: | |
Cc: | tonn81@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
django.core.urlresolvers.reverse does not work with function and namespace.
Example:
>>> from payment.views import * >>> reverse('payment:payment.views.paypal_express_confirm') # resolve using namespace and view path '/payment/paypal_express/confirm/' >>> paypal_express_confirm # check function is there <function paypal_express_confirm at 0x39f7668> >>> reverse(paypal_express_confirm) # try direct resolve Traceback (most recent call last): File "<console>", line 1, in <module> File "django/core/urlresolvers.py", line 476, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) File "django/core/urlresolvers.py", line 396, in _reverse_with_prefix "arguments '%s' not found." % (lookup_view_s, args, kwargs)) NoReverseMatch: Reverse for 'payment.views.paypal_express_confirm' with arguments '()' and keyword arguments '{}' not found. >>> reverse(paypal_express_confirm, current_app='payment') # try with current_app given Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/tonnzor/projects/reelport-application/picturepipe/src/django/django/core/urlresolvers.py", line 476, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) File "/home/tonnzor/projects/reelport-application/picturepipe/src/django/django/core/urlresolvers.py", line 396, in _reverse_with_prefix "arguments '%s' not found." % (lookup_view_s, args, kwargs)) NoReverseMatch: Reverse for 'payment.views.paypal_express_confirm' with arguments '()' and keyword arguments '{}' not found. >>> reverse(paypal_express_confirm, urlconf='payment.urls') # try app urls without namespaces or something '/paypal_express/confirm/'
Sure it works when you open /payment/paypal_express/confirm/
My project:
/urls.py:
from django.conf.urls import * urlpatterns = patterns('', url(r'^payment/', include('payment.urls', namespace='payment', app_name='payment')), )
payment/urls.py:
from django.conf.urls.defaults import * urlpatterns = patterns('payment.views', url(r'^paypal_express/confirm/$', 'paypal_express_confirm'), )
payment/views.py:
from django.http import HttpResponse def paypal_express_confirm(request): return HttpResponse('Confirm')
Sorry, I didn't get hands to test on master -- but I'm pretty sure it is still there.
Change History (6)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
Component: | Core (URLs) → Documentation |
---|---|
Type: | Bug → Cleanup/optimization |
Namespaces are designed to be used with URL names, which is a much better way of reversing anyway. I don't think this should be changed, but perhaps docs could be clarified?
comment:3 by , 11 years ago
But how could I reverse my view by function when using namespace?
Then why do we need current_app if it is not able to find view by it?
comment:4 by , 11 years ago
Since Django always state that I could use function instead of its name for routing (i.e. in urls.py, reverse and redirect) -- I assume it is a bug that it cannot be used in combination with namespace. Not documentation bug.
comment:6 by , 11 years ago
Is this actually a duplicate? The original ticket is about passing callable view functions to reverse()
. This ticket is about passing a dotted path reference to a view function, with a namespace, as a string. With the original ticket, there is no way to specify a namespace. With this ticket, a namespace is being specified.
If you use view functions in payment/urls.py -- it works exactly the same.
payment/urls.py: