Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#17376 closed Bug (worksforme)

reverse() function does not work in the middleware

Reported by: stalker@… Owned by: nobody
Component: Core (URLs) Version: 1.3
Severity: Normal Keywords: reverse, middleware
Cc: piotr@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

urls.py:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    #(r'^comments/', include('comments.urls')),
    .....
    url(r'^index.html$', 'root.views.ShowRoot'),
    url(r'^$', 'root.views.ShowRoot', name='index'),
    #-----------------------------------------
)


Middleware:

from django.core.urlresolvers import reverse

class AddSomeJS(object):
  def process_request(self, Request):
    print('=>{}'.format(reverse('index')))

  def process_response(self, Request, Response):
    print('=>{}'.format(reverse('index')))
    return Response

In process_request reverse works properly. But in the process_response we get error:

ImproperlyConfigured at /admin/articles/article4dealers/4/

The included urlconf urls doesn't have any patterns in it

Request Method: 	GET
Request URL: 	http://localhost:8000/admin/articles/article4dealers/4/
Django Version: 	1.3.1
Exception Type: 	ImproperlyConfigured
Exception Value: 	

The included urlconf urls doesn't have any patterns in it

Exception Location: 	C:\WEB\VE\kler\lib\site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
Python Executable: 	C:\WEB\VE\kler\Scripts\python.exe
Python Version: 	2.7.1

...........................

    Traceback:
    File "C:\WEB\VE\kler\lib\site-packages\django\core\handlers\base.py" in get_response
      178.                 response = middleware_method(request, response)
    File "C:\WEB\kler\asv_media\middleware.py" in process_response
      62.         print('=>{}'.format(reverse('index')))
    File "C:\WEB\VE\kler\lib\site-packages\django\core\urlresolvers.py" in reverse
      391.             *args, **kwargs)))
    File "C:\WEB\VE\kler\lib\site-packages\django\core\urlresolvers.py" in reverse
      312.         possibilities = self.reverse_dict.getlist(lookup_view)
    File "C:\WEB\VE\kler\lib\site-packages\django\core\urlresolvers.py" in _get_reverse_dict
      229.             self._populate()
    File "C:\WEB\VE\kler\lib\site-packages\django\core\urlresolvers.py" in _populate
      197.         for pattern in reversed(self.url_patterns):
    File "C:\WEB\VE\kler\lib\site-packages\django\core\urlresolvers.py" in _get_url_patterns
      283.             raise ImproperlyConfigured("The included urlconf %s doesn't have any patterns in it" % self.urlconf_name)

    Exception Type: ImproperlyConfigured at /admin/articles/article4dealers/4/
    Exception Value: The included urlconf urls doesn't have any patterns in it

Change History (7)

comment:1 by Karen Tracey, 13 years ago

The finally clause that does urlresolvers.set_urlconf(None) to "Reset URLconf for this thread on the way out for complete isolation of request.urlconf" happens before response middleware is applied. "on the way out" is a bit later in the code (at least now) than where that finally clause is placed. Seems that reset should be done after the response middleware has been applied.

comment:2 by Karen Tracey, 13 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Aymeric Augustin, 13 years ago

Component: UncategorizedCore (URLs)

comment:4 by Piotr Banaszkiewicz, 13 years ago

Cc: piotr@… added
Resolution: worksforme
Status: newclosed

Cannot reproduce

comment:5 by stalker@…, 13 years ago

I am sorry, but what You can't reproduce?

comment:6 by Piotr Banaszkiewicz, 13 years ago

With Django 1.4a I am unable to reproduce your error. To be exact here are some lines from ./manage.py runserver output:

=>/ in
=>/ out
[04/Feb/2012 12:58:43] "GET /admin/auth/group/ HTTP/1.1" 200 3253
=>/ in
=>/ out

I used almost the same as yours code, and my urls.py is:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'asdf.views.index', name="index"),
)

comment:7 by stalker@…, 13 years ago

thanks! I will be wait new release...

Note: See TracTickets for help on using tickets.
Back to Top