Code

Opened 2 years ago

Closed 2 years ago

Last modified 2 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

Attachments (0)

Change History (7)

comment:1 Changed 2 years ago by kmtracey

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

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

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 2 years ago by aaugustin

  • Component changed from Uncategorized to Core (URLs)

comment:4 Changed 2 years ago by pbnan

  • Cc piotr@… added
  • Resolution set to worksforme
  • Status changed from new to closed

Cannot reproduce

comment:5 Changed 2 years ago by stalker@…

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

comment:6 Changed 2 years ago by pbnan

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 Changed 2 years ago by stalker@…

thanks! I will be wait new release...

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.