diff --git a/django/views/debug.py b/django/views/debug.py index 8909c1f64f..256f7cd662 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -51,9 +51,20 @@ def get_template_exception_info(exc_type, exc_value, tb): exc_info = hasattr(exc_value, 'exc_info') and exc_value.exc_info or (exc_type, exc_value, tb) return exc_info + (template_info,) +def get_safe_settings(): + "Returns a dictionary of the settings module, with sensitive settings blurred out." + settings_dict = {} + for k in dir(settings): + if k.isupper(): + if HIDDEN_SETTINGS.search(k): + settings_dict[k] = '********************' + else: + settings_dict[k] = getattr(settings, k) + return settings_dict + def technical_500_response(request, exc_type, exc_value, tb): """ - Create a technical server error response. The last three arguments are + Create a technical server error response. The last three arguments are the values returned from sys.exc_info() and friends. """ template_info = None @@ -98,16 +109,6 @@ def technical_500_response(request, exc_type, exc_value, tb): }) tb = tb.tb_next - # Turn the settings module into a dict, filtering out anything that - # matches HIDDEN_SETTINGS along the way. - settings_dict = {} - for k in dir(settings): - if k.isupper(): - if HIDDEN_SETTINGS.search(k): - settings_dict[k] = '********************' - else: - settings_dict[k] = getattr(settings, k) - t = Template(TECHNICAL_500_TEMPLATE) c = Context({ 'exception_type': exc_type.__name__, @@ -116,7 +117,7 @@ def technical_500_response(request, exc_type, exc_value, tb): 'lastframe': frames[-1], 'request': request, 'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", - 'settings': settings_dict, + 'settings': get_safe_settings(), 'template_info': template_info, 'template_does_not_exist': template_does_not_exist, 'loader_debug_info': loader_debug_info, @@ -124,11 +125,7 @@ def technical_500_response(request, exc_type, exc_value, tb): return HttpResponseServerError(t.render(c), mimetype='text/html') def technical_404_response(request, exception): - """ - Create a technical 404 error response. The exception should be the Http404 - exception. - """ - urlconf_is_empty = False + "Create a technical 404 error response. The exception should be the Http404." try: tried = exception.args[0]['tried'] except (IndexError, TypeError): @@ -136,17 +133,24 @@ def technical_404_response(request, exception): else: if not tried: # tried exists but is an empty list. The URLconf must've been empty. - urlconf_is_empty = True + return empty_urlconf(request) t = Template(TECHNICAL_404_TEMPLATE) c = Context({ 'root_urlconf': settings.ROOT_URLCONF, 'urlpatterns': tried, - 'urlconf_is_empty': urlconf_is_empty, 'reason': str(exception), 'request': request, 'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", - 'settings': dict([(k, getattr(settings, k)) for k in dir(settings) if k.isupper()]), + 'settings': get_safe_settings(), + }) + return HttpResponseNotFound(t.render(c), mimetype='text/html') + +def empty_urlconf(request): + "Create an empty URLconf 404 error response." + t = Template(EMPTY_URLCONF_TEMPLATE) + c = Context({ + 'project_name': settings.SETTINGS_MODULE.split('.')[0] }) return HttpResponseNotFound(t.render(c), mimetype='text/html') @@ -539,23 +543,19 @@ TECHNICAL_404_TEMPLATE = """
- {% if urlconf_is_empty %} -

Your URLconf, {{ settings.ROOT_URLCONF }}, was empty.

+ {% if urlpatterns %} +

+ Using the URLconf defined in {{ settings.ROOT_URLCONF }}, + Django tried these URL patterns, in this order: +

+
    + {% for pattern in urlpatterns %} +
  1. {{ pattern|escape }}
  2. + {% endfor %} +
+

The current URL, {{ request.path }}, didn't match any of these.

{% else %} - {% if urlpatterns %} -

- Using the URLconf defined in {{ settings.ROOT_URLCONF }}, - Django tried these URL patterns, in this order: -

-
    - {% for pattern in urlpatterns %} -
  1. {{ pattern|escape }}
  2. - {% endfor %} -
-

The current URL, {{ request.path }}, didn't match any of these.

- {% else %} -

{{ reason|escape }}

- {% endif %} +

{{ reason|escape }}

{% endif %}
@@ -569,3 +569,55 @@ TECHNICAL_404_TEMPLATE = """ """ + +EMPTY_URLCONF_TEMPLATE = """ + + + + Welcome to Django + + + + +
+

It worked!

+

Congratulations on your first Django-powered page.

+
+ +
+

Of course, you haven't actually done any work yet. Here's what to do next:

+ +
+ +
+

+ You're seeing this message because you have DEBUG = True in your + Django settings file and you haven't configured any URLs. Get to work! +

+
+ +"""