Opened 17 years ago

Closed 14 years ago

Last modified 12 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: dev
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@…> 17 years ago.

Download all attachments as: .zip

Change History (16)

by Thomas Güttler <hv@…>, 17 years ago

Attachment: urlresolvers.patch added

comment:1 by flother, 17 years ago

Needs documentation: set
Triage Stage: UnreviewedDesign 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 by anonymous, 16 years ago

See #5588, too.

comment:3 by Michael Radziej, 16 years ago

Resolution: duplicate
Status: newclosed

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

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

comment:4 by Collin Grady, 16 years ago

Resolution: duplicate
Status: closedreopened

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

comment:5 by Thomas Güttler <hv@…>, 16 years ago

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 by Thomas Güttler <hv@…>, 16 years ago

Cc: hv@… added

comment:7 by Malcolm Tredinnick, 16 years ago

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 by Thomas Güttler <hv@…>, 16 years ago

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 by pb@…, 16 years ago

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 by pmarti, 14 years ago

+1, I've just been bitten by it

comment:11 by John Jacobsen, 14 years ago

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

comment:12 by ReidKleckner, 14 years ago

Resolution: fixed
Status: reopenedclosed

+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 by ReidKleckner, 14 years ago

Resolution: fixed
Status: closedreopened

Erg, silly trac.

comment:14 by Russell Keith-Magee, 14 years ago

Resolution: fixed
Status: reopenedclosed

(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 by Thomas Güttler, 12 years ago

Cc: hv@… removed
Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset
Note: See TracTickets for help on using tickets.
Back to Top