Django

Code

Changeset 1565

Show
Ignore:
Timestamp:
12/07/05 00:02:09 (3 years ago)
Author:
adrian
Message:

Added 'It worked' page, in a empty_urlconf() view in views/debug.py. It's called if the URLconf is empty.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/views/debug.py

    r1552 r1565  
    5252    return exc_info + (template_info,) 
    5353 
     54def get_safe_settings(): 
     55    "Returns a dictionary of the settings module, with sensitive settings blurred out." 
     56    settings_dict = {} 
     57    for k in dir(settings): 
     58        if k.isupper(): 
     59            if HIDDEN_SETTINGS.search(k): 
     60                settings_dict[k] = '********************' 
     61            else: 
     62                settings_dict[k] = getattr(settings, k) 
     63    return settings_dict 
     64 
    5465def technical_500_response(request, exc_type, exc_value, tb): 
    5566    """ 
    56     Create a technical server error response. The last three arguments are 
     67    Create a technical server error response. The last three arguments are 
    5768    the values returned from sys.exc_info() and friends. 
    5869    """ 
     
    99110        tb = tb.tb_next 
    100111 
    101     # Turn the settings module into a dict, filtering out anything that 
    102     # matches HIDDEN_SETTINGS along the way. 
    103     settings_dict = {} 
    104     for k in dir(settings): 
    105         if k.isupper(): 
    106             if HIDDEN_SETTINGS.search(k): 
    107                 settings_dict[k] = '********************' 
    108             else: 
    109                 settings_dict[k] = getattr(settings, k) 
    110  
    111112    t = Template(TECHNICAL_500_TEMPLATE) 
    112113    c = Context({ 
     
    117118        'request': request, 
    118119        'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", 
    119         'settings': settings_dict
     120        'settings': get_safe_settings()
    120121        'template_info': template_info, 
    121122        'template_does_not_exist': template_does_not_exist, 
     
    125126 
    126127def technical_404_response(request, exception): 
    127     """ 
    128     Create a technical 404 error response.  The exception should be the Http404 
    129     exception. 
    130     """ 
    131     urlconf_is_empty = False 
     128    "Create a technical 404 error response. The exception should be the Http404." 
    132129    try: 
    133130        tried = exception.args[0]['tried'] 
     
    137134        if not tried: 
    138135            # tried exists but is an empty list. The URLconf must've been empty. 
    139             urlconf_is_empty = True 
     136            return empty_urlconf(request) 
    140137 
    141138    t = Template(TECHNICAL_404_TEMPLATE) 
     
    143140        'root_urlconf': settings.ROOT_URLCONF, 
    144141        'urlpatterns': tried, 
    145         'urlconf_is_empty': urlconf_is_empty, 
    146142        'reason': str(exception), 
    147143        'request': request, 
    148144        'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", 
    149         'settings': dict([(k, getattr(settings, k)) for k in dir(settings) if k.isupper()]), 
     145        'settings': get_safe_settings(), 
     146    }) 
     147    return HttpResponseNotFound(t.render(c), mimetype='text/html') 
     148 
     149def empty_urlconf(request): 
     150    "Create an empty URLconf 404 error response." 
     151    t = Template(EMPTY_URLCONF_TEMPLATE) 
     152    c = Context({ 
     153        'project_name': settings.SETTINGS_MODULE.split('.')[0] 
    150154    }) 
    151155    return HttpResponseNotFound(t.render(c), mimetype='text/html') 
     
    540544  </div> 
    541545  <div id="info"> 
    542     {% if urlconf_is_empty %} 
    543       <p>Your URLconf, <code>{{ settings.ROOT_URLCONF }}</code>, was empty.</p> 
     546    {% if urlpatterns %} 
     547      <p> 
     548      Using the URLconf defined in <code>{{ settings.ROOT_URLCONF }}</code>, 
     549      Django tried these URL patterns, in this order: 
     550      </p> 
     551      <ol> 
     552        {% for pattern in urlpatterns %} 
     553          <li>{{ pattern|escape }}</li> 
     554        {% endfor %} 
     555      </ol> 
     556      <p>The current URL, <code>{{ request.path }}</code>, didn't match any of these.</p> 
    544557    {% else %} 
    545       {% if urlpatterns %} 
    546         <p> 
    547         Using the URLconf defined in <code>{{ settings.ROOT_URLCONF }}</code>, 
    548         Django tried these URL patterns, in this order: 
    549         </p> 
    550         <ol> 
    551           {% for pattern in urlpatterns %} 
    552             <li>{{ pattern|escape }}</li> 
    553           {% endfor %} 
    554         </ol> 
    555         <p>The current URL, <code>{{ request.path }}</code>, didn't match any of these.</p> 
    556       {% else %} 
    557         <p>{{ reason|escape }}</p> 
    558       {% endif %} 
     558      <p>{{ reason|escape }}</p> 
    559559    {% endif %} 
    560560  </div> 
     
    570570</html> 
    571571""" 
     572 
     573EMPTY_URLCONF_TEMPLATE = """ 
     574<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
     575<html lang="en"><head> 
     576  <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
     577  <meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title> 
     578  <style type="text/css"> 
     579    html * { padding:0; margin:0; } 
     580    body * { padding:10px 20px; } 
     581    body * * { padding:0; } 
     582    body { font:small sans-serif; } 
     583    body>div { border-bottom:1px solid #ddd; } 
     584    h1 { font-weight:normal; } 
     585    h2 { margin-bottom:.8em; } 
     586    h2 span { font-size:80%; color:#666; font-weight:normal; } 
     587    h3 { margin:1em 0 .5em 0; } 
     588    h4 { margin:0 0 .5em 0; font-weight: normal; } 
     589    table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; } 
     590    tbody td, tbody th { vertical-align:top; padding:2px 3px; } 
     591    thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; } 
     592    tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; } 
     593    ul { margin-left: 2em; margin-top: 1em; } 
     594    #summary { background: #e0ebff; } 
     595    #summary h2 { font-weight: normal; color: #666; } 
     596    #explanation { background:#eee; } 
     597    #instructions { background:#f6f6f6; } 
     598    #summary table { border:none; background:transparent; } 
     599  </style> 
     600</head> 
     601 
     602<body> 
     603<div id="summary"> 
     604  <h1>It worked!</h1> 
     605  <h2>Congratulations on your first Django-powered page.</h2> 
     606</div> 
     607 
     608<div id="instructions"> 
     609  <p>Of course, you haven't actually done any work yet. Here's what to do next:</p> 
     610  <ul> 
     611    <li>Edit the <code>DATABASE_*</code> settings in <code>{{ project_name }}/settings.py</code>.</li> 
     612    <li>Start your first app by running <code>{{ project_name }}/manage.py startapp [appname]</code>.</li> 
     613  </ul> 
     614</div> 
     615 
     616<div id="explanation"> 
     617  <p> 
     618    You're seeing this message because you have <code>DEBUG = True</code> in your 
     619    Django settings file and you haven't configured any URLs. Get to work! 
     620  </p> 
     621</div> 
     622</body></html> 
     623"""