#5350 closed Uncategorized (fixed)
urlresolvers: Fallback, if urls.py don't have handle404 and friends
Reported by: | 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)
Change History (16)
by , 17 years ago
Attachment: | urlresolvers.patch added |
---|
comment:1 by , 17 years ago
Needs documentation: | set |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:3 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
You shouldn't unless your first name is Adrian ;-)
Closed as duplicate of #5588 (and thanks to the anonymous pointer!)
comment:4 by , 17 years ago
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
#5588 came later, and this one contains a patch, so it seems the better candidate to remain open :)
comment:5 by , 17 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 , 17 years ago
Cc: | added |
---|
comment:7 by , 17 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 , 17 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 , 17 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:12 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → 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:14 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
comment:15 by , 13 years ago
Cc: | removed |
---|---|
Easy pickings: | unset |
Severity: | → Normal |
Type: | → Uncategorized |
UI/UX: | unset |
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.