Code

Opened 7 years ago

Closed 4 years ago

Last modified 3 years ago

#5350 closed Uncategorized (fixed)

urlresolvers: Fallback, if urls.py don't have handle404 and friends

Reported by: Thomas Güttler <hv@…> Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

since I don't like * imports, my urls.py looks like this:

# Django Imports
from django.conf.urls.defaults import patterns, include

urlpatterns = patterns(
    '',
    (r'^modwork/admin/', include('django.contrib.admin.urls')),
     ...

If DEBUG==False, I get an exception::

# Revision 6051.
# Sept. 6 2007
Mod_python error: "PythonHandler django.core.handlers.modpython"
Traceback (most recent call last):
  File "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
    result = object(req)
  File "/home/modarch/modwork/django/core/handlers/modpython.py", line 178, in handler
    return ModPythonHandler()(req)
  File "/home/modarch/modwork/django/core/handlers/modpython.py", line 151, in __call__
    response = self.get_response(request)
  File "/home/modarch/modwork/django/core/handlers/base.py", line 102, in get_response
    callback, param_dict = resolver.resolve404()
  File "/home/modarch/modwork/django/core/urlresolvers.py", line 271, in resolve404
    return self._resolve_special('404')
  File "/home/modarch/modwork/django/core/urlresolvers.py", line 263, in _resolve_special
    callback = getattr(self.urlconf_module, 'handler%s' % view_type)
AttributeError: 'module' object has no attribute 'handler404'

Attached is a small patch, which uses the handler from urls.defaults, if they are unkown.


Attachments (1)

urlresolvers.patch (822 bytes) - added by Thomas Güttler <hv@…> 7 years ago.

Download all attachments as: .zip

Change History (16)

Changed 7 years ago by Thomas Güttler <hv@…>

comment:1 Changed 7 years ago by flother

  • Needs documentation set
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

Can you not just import:

from django.conf.urls.defaults import handler404, handler500, include, patterns

or:

from django.conf.urls.defaults import handler404, handler500, include, patterns, url

if using Subversion trunk? That will solve your problem.

comment:2 Changed 7 years ago by anonymous

See #5588, too.

comment:3 Changed 7 years ago by mir

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

You shouldn't unless your first name is Adrian ;-)

Closed as duplicate of #5588 (and thanks to the anonymous pointer!)

comment:4 Changed 7 years ago by cgrady

  • Resolution duplicate deleted
  • Status changed from closed to reopened

#5588 came later, and this one contains a patch, so it seems the better candidate to remain open :)

comment:5 Changed 7 years ago by Thomas Güttler <hv@…>

Yes, I can use this:

from django.conf.urls.defaults import handler404, handler500, include, patterns, url

But this is black magic: handler404 gets only imported, but never used. The first
who cares for clean code (and does not know the black magic) will remove the
unneeded import of handler404 and handler500.

That's way think this patch should be applied: If there is no handler-method
in urls.py, the one from django.conf.urls.defaults should be used.

comment:6 Changed 7 years ago by Thomas Güttler <hv@…>

  • Cc hv@… added

comment:7 Changed 7 years ago by mtredinnick

I don't like the approach here (it feels like explicit is better than implicit in this case). Would prefer we come up with a way to indicate that handler404 and handler500 need to be imported if not supplied directly.

comment:8 Changed 7 years ago by Thomas Güttler <hv@…>

Yes, that's a good solution, too.

May raise an AssertionError if they are missing?

BTW: I never do 'import ... *', but it is part of the tutorial. The current situation is
black magic.

comment:9 Changed 6 years ago by pb@…

This patch actually seems like a net "explicit over implicit" improvement to me: making the default 4040 and 500 handlers available automatically would mean that most URLconfs would only need to import two or three identifiers from django.conf.urls.defaults, which would weaken the argument for keeping Django's sole remaining wildcard import.

comment:10 Changed 5 years ago by pmarti

+1, I've just been bitten by it

comment:11 Changed 4 years ago by eigenhombre

+1, I've just been bitten as well.

comment:12 Changed 4 years ago by ReidKleckner

  • Resolution set to fixed
  • Status changed from reopened to closed

+1, I just got bitten by it as well. As a matter of habit, I remove import *s so that I can use pyflakes to tell me if I'm using undefined names in my code.

comment:13 Changed 4 years ago by ReidKleckner

  • Resolution fixed deleted
  • Status changed from closed to reopened

Erg, silly trac.

comment:14 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [13590]) Fixed #5350 -- Added fallback to default 404/500 handlers when they're not explicitly specified (or imported) in a urls.py file. Thanks to Thomas Güttler for the report and initial patch.

comment:15 Changed 3 years ago by guettli

  • Cc hv@… removed
  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

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.