python - Django: failed to overwrite handler404 -
part urls.py (for testing):
old_handler404 = handler404 def custom_page_not_found(request, *args, **kwargs): django.http import httpresponsenotfound if request.path.startswith('/myapp/api'): try: return httpresponsenotfound( json.dumps( {'info': 'page not found'} ), content_type='application/json') except exception e: print 'exception:', e #return httpresponsenotfound('page not found', content_type='text/html; charset=utf-8') try: res = old_handler404( request, *args, **kwargs ) except exception e: print 'exception:', e return res handler404 = custom_page_not_found
trying overwrite existing 404handler, rest apis (urls starting /myapp/api
), return 404 , error messages in json if urls not matched, remainings, return 404 , existing not found
pages if url not matched.
for /myapp/api
, works fine. remainings, returned 500
instead of expected 404
. no exception information shown.
any comments welcomed. thanks
update
exception: 'str' object not callable
update
how following codes? side-effects? in fact, in django app myapp
, there several sub-apps. sub-apps finished others, , api
sub-app finished me. not change/touch others' logic , codes. how following codes? welcomes welcomed. thanks.
from django.views.defaults import page_not_found old_page_not_found #old_handler404 = django.views.defaults.page_not_found def custom_page_not_found(request, *args, **kwargs): django.http import httpresponsenotfound if request.path.startswith('/myapp/api'): try: return httpresponsenotfound( json.dumps( {'info': 'page not found'} ), content_type='application/json') except exception e: print 'exception:', e #return httpresponsenotfound('page not found', content_type='text/html; charset=utf-8') try: res = old_page_not_found( request, *args, **kwargs) except exception e: print 'exception:', e return res
Comments
Post a Comment