Code

Opened 5 months ago

Closed 5 months ago

Last modified 4 months ago

#21899 closed Cleanup/optimization (duplicate)

Reverse does not work with function and namespace

Reported by: tonnzor 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.

Attachments (0)

Change History (6)

comment:1 Changed 5 months ago by tonnzor

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

If you use view functions in payment/urls.py -- it works exactly the same.

payment/urls.py:

from django.conf.urls.defaults import *
from payment.views import *

urlpatterns = patterns('',
    url(r'^paypal_express/confirm/$', paypal_express_confirm),
)

comment:2 Changed 5 months ago by mjtamlyn

  • Component changed from Core (URLs) to Documentation
  • Type changed from Bug to 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 Changed 5 months ago by tonnzor

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 Changed 5 months ago by tonnzor

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:5 Changed 5 months ago by bpeschier

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #17914

comment:6 Changed 4 months ago by mrmachine

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.