Code

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#4319 closed (invalid)

urlresolvers.RegexURLPattern._get_callback doesn't handle ValueError

Reported by: django@… Owned by: adrian
Component: Core (Other) Version: master
Severity: Keywords: url resolver _get_callback
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by mtredinnick)

It seems import raises a ValueError if the module name is empty. This can occur in _get_callback and isn't handled properly:

Using a named URL pattern "blog-post" in a permalink-decorated get_absolute_url method causes the exception detailed below.

Switching the name to "blog.post" instead fools the reverse functions into allowing it because _get_callback receives a non-empty module name.

Traceback (most recent call last):
File "d:\python25\Lib\site-packages\django\template\__init__.py" in render_node
  741. result = node.render(context)
File "d:\python25\Lib\site-packages\django\template\defaulttags.py" in render
  125. nodelist.append(node.render(context))
File "d:\python25\Lib\site-packages\django\template\__init__.py" in render
  791. output = self.filter_expression.resolve(context)
File "d:\python25\Lib\site-packages\django\template\__init__.py" in resolve
  576. obj = resolve_variable(self.var, context)
File "d:\python25\Lib\site-packages\django\template\__init__.py" in resolve_variable
  674. current = current()
File "d:\python25\Lib\site-packages\django\utils\functional.py" in _curried
  3. return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
File "d:\python25\Lib\site-packages\django\db\models\base.py" in get_absolute_url
  453. return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self)
File "d:\python25\Lib\site-packages\django\db\models\__init__.py" in inner
  27. return reverse(bits[0], None, *bits[1:3])
File "d:\python25\Lib\site-packages\django\core\urlresolvers.py" in reverse
  251. return '/' + resolver.reverse(viewname, *args, **kwargs)
File "d:\python25\Lib\site-packages\django\core\urlresolvers.py" in reverse
  222. return pattern.reverse_helper(lookup_view, *args, **kwargs)
File "d:\python25\Lib\site-packages\django\core\urlresolvers.py" in reverse_helper
  233. sub_match = self.reverse(lookup_view, *args, **kwargs)
File "d:\python25\Lib\site-packages\django\core\urlresolvers.py" in reverse
  225. elif pattern.callback == lookup_view or pattern.name == lookup_view:
File "d:\python25\Lib\site-packages\django\core\urlresolvers.py" in _get_callback
  134. self._callback = getattr(__import__(mod_name, {}, {}, ['']), func_name)

  ValueError at /blog/articles/
  Empty module name

Attachments (0)

Change History (4)

comment:1 Changed 7 years ago by django@…

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

Can't get this to work at all in fact.

Switching to "blog.post" (an existing named urlpattern) causes NoReverseMatch exceptions.
Using the generic view name "django.views.generic.date_based.object_detail" causes ValueErrors because of empty module name in import.

comment:2 Changed 7 years ago by mtredinnick

  • Description modified (diff)

Could you give an example of a url pattern and the associated get_absolute_url() method that causes this problem, please?

comment:3 Changed 7 years ago by django@…

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

Sorry, false alarm. I had an URL pattern with a blank view function in there:

  (r'articles/category/(?P<category>.+)/page/(?P<page>.+)', ''),

which is what was causing the problem. I'd assumed that this was safe enough during development - I was filling in the views bit by bit, but I guess the reverse resolution checks each URL pattern's view function so problems showed up when I used the permalink decorator.

Further down, I had the named pattern:

                        url(r'articles/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>.+)/$',
                            'object_detail',
                            dict(posts_dict, slug_field = 'slug', month_format = '%m'),
                            'blog-post'),

which I had thought was causing the problems.

(However I'm still not convinced that the empty view functions shouldn't just be disregarded by the reverse URL processing. My feeling is that it's either valid or invalid to have an empty string in there. If it's valid then it should just be silently passed over by the reverse URL processing. If it's invalid you shouldn't be unaware of it until you start using reverse lookups.)

comment:4 Changed 7 years ago by mtredinnick

(In [5360]) Report an error if urlpatterns contain a string view name and it is empty.
Refs #4319.

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.