PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/django-1.3/django/views/debug.py

https://github.com/theosp/google_appengine
Python | 894 lines | 856 code | 18 blank | 20 comment | 27 complexity | ed0dcac0e38cf9773f96784f3899f059 MD5 | raw file
  1. import datetime
  2. import os
  3. import re
  4. import sys
  5. import types
  6. from django.conf import settings
  7. from django.http import HttpResponse, HttpResponseServerError, HttpResponseNotFound
  8. from django.template import (Template, Context, TemplateDoesNotExist,
  9. TemplateSyntaxError)
  10. from django.template.defaultfilters import force_escape, pprint
  11. from django.utils.html import escape
  12. from django.utils.importlib import import_module
  13. from django.utils.encoding import smart_unicode, smart_str
  14. HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD|PROFANITIES_LIST|SIGNATURE')
  15. def linebreak_iter(template_source):
  16. yield 0
  17. p = template_source.find('\n')
  18. while p >= 0:
  19. yield p+1
  20. p = template_source.find('\n', p+1)
  21. yield len(template_source) + 1
  22. def cleanse_setting(key, value):
  23. """Cleanse an individual setting key/value of sensitive content.
  24. If the value is a dictionary, recursively cleanse the keys in
  25. that dictionary.
  26. """
  27. try:
  28. if HIDDEN_SETTINGS.search(key):
  29. cleansed = '********************'
  30. else:
  31. if isinstance(value, dict):
  32. cleansed = dict((k, cleanse_setting(k, v)) for k,v in value.items())
  33. else:
  34. cleansed = value
  35. except TypeError:
  36. # If the key isn't regex-able, just return as-is.
  37. cleansed = value
  38. return cleansed
  39. def get_safe_settings():
  40. "Returns a dictionary of the settings module, with sensitive settings blurred out."
  41. settings_dict = {}
  42. for k in dir(settings):
  43. if k.isupper():
  44. settings_dict[k] = cleanse_setting(k, getattr(settings, k))
  45. return settings_dict
  46. def technical_500_response(request, exc_type, exc_value, tb):
  47. """
  48. Create a technical server error response. The last three arguments are
  49. the values returned from sys.exc_info() and friends.
  50. """
  51. reporter = ExceptionReporter(request, exc_type, exc_value, tb)
  52. html = reporter.get_traceback_html()
  53. return HttpResponseServerError(html, mimetype='text/html')
  54. class ExceptionReporter(object):
  55. """
  56. A class to organize and coordinate reporting on exceptions.
  57. """
  58. def __init__(self, request, exc_type, exc_value, tb, is_email=False):
  59. self.request = request
  60. self.exc_type = exc_type
  61. self.exc_value = exc_value
  62. self.tb = tb
  63. self.is_email = is_email
  64. self.template_info = None
  65. self.template_does_not_exist = False
  66. self.loader_debug_info = None
  67. # Handle deprecated string exceptions
  68. if isinstance(self.exc_type, basestring):
  69. self.exc_value = Exception('Deprecated String Exception: %r' % self.exc_type)
  70. self.exc_type = type(self.exc_value)
  71. def get_traceback_html(self):
  72. "Return HTML code for traceback."
  73. if self.exc_type and issubclass(self.exc_type, TemplateDoesNotExist):
  74. from django.template.loader import template_source_loaders
  75. self.template_does_not_exist = True
  76. self.loader_debug_info = []
  77. for loader in template_source_loaders:
  78. try:
  79. module = import_module(loader.__module__)
  80. if hasattr(loader, '__class__'):
  81. source_list_func = loader.get_template_sources
  82. else: # NOTE: Remember to remove this branch when we deprecate old template loaders in 1.4
  83. source_list_func = module.get_template_sources
  84. # NOTE: This assumes exc_value is the name of the template that
  85. # the loader attempted to load.
  86. template_list = [{'name': t, 'exists': os.path.exists(t)} \
  87. for t in source_list_func(str(self.exc_value))]
  88. except (ImportError, AttributeError):
  89. template_list = []
  90. if hasattr(loader, '__class__'):
  91. loader_name = loader.__module__ + '.' + loader.__class__.__name__
  92. else: # NOTE: Remember to remove this branch when we deprecate old template loaders in 1.4
  93. loader_name = loader.__module__ + '.' + loader.__name__
  94. self.loader_debug_info.append({
  95. 'loader': loader_name,
  96. 'templates': template_list,
  97. })
  98. if (settings.TEMPLATE_DEBUG and hasattr(self.exc_value, 'source') and
  99. isinstance(self.exc_value, TemplateSyntaxError)):
  100. self.get_template_exception_info()
  101. frames = self.get_traceback_frames()
  102. for i, frame in enumerate(frames):
  103. if 'vars' in frame:
  104. frame['vars'] = [(k, force_escape(pprint(v))) for k, v in frame['vars']]
  105. frames[i] = frame
  106. unicode_hint = ''
  107. if self.exc_type and issubclass(self.exc_type, UnicodeError):
  108. start = getattr(self.exc_value, 'start', None)
  109. end = getattr(self.exc_value, 'end', None)
  110. if start is not None and end is not None:
  111. unicode_str = self.exc_value.args[1]
  112. unicode_hint = smart_unicode(unicode_str[max(start-5, 0):min(end+5, len(unicode_str))], 'ascii', errors='replace')
  113. from django import get_version
  114. t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 template')
  115. c = Context({
  116. 'is_email': self.is_email,
  117. 'unicode_hint': unicode_hint,
  118. 'frames': frames,
  119. 'request': self.request,
  120. 'settings': get_safe_settings(),
  121. 'sys_executable': sys.executable,
  122. 'sys_version_info': '%d.%d.%d' % sys.version_info[0:3],
  123. 'server_time': datetime.datetime.now(),
  124. 'django_version_info': get_version(),
  125. 'sys_path' : sys.path,
  126. 'template_info': self.template_info,
  127. 'template_does_not_exist': self.template_does_not_exist,
  128. 'loader_debug_info': self.loader_debug_info,
  129. })
  130. # Check whether exception info is available
  131. if self.exc_type:
  132. c['exception_type'] = self.exc_type.__name__
  133. if self.exc_value:
  134. c['exception_value'] = smart_unicode(self.exc_value, errors='replace')
  135. if frames:
  136. c['lastframe'] = frames[-1]
  137. return t.render(c)
  138. def get_template_exception_info(self):
  139. origin, (start, end) = self.exc_value.source
  140. template_source = origin.reload()
  141. context_lines = 10
  142. line = 0
  143. upto = 0
  144. source_lines = []
  145. before = during = after = ""
  146. for num, next in enumerate(linebreak_iter(template_source)):
  147. if start >= upto and end <= next:
  148. line = num
  149. before = escape(template_source[upto:start])
  150. during = escape(template_source[start:end])
  151. after = escape(template_source[end:next])
  152. source_lines.append( (num, escape(template_source[upto:next])) )
  153. upto = next
  154. total = len(source_lines)
  155. top = max(1, line - context_lines)
  156. bottom = min(total, line + 1 + context_lines)
  157. self.template_info = {
  158. 'message': self.exc_value.args[0],
  159. 'source_lines': source_lines[top:bottom],
  160. 'before': before,
  161. 'during': during,
  162. 'after': after,
  163. 'top': top,
  164. 'bottom': bottom,
  165. 'total': total,
  166. 'line': line,
  167. 'name': origin.name,
  168. }
  169. def _get_lines_from_file(self, filename, lineno, context_lines, loader=None, module_name=None):
  170. """
  171. Returns context_lines before and after lineno from file.
  172. Returns (pre_context_lineno, pre_context, context_line, post_context).
  173. """
  174. source = None
  175. if loader is not None and hasattr(loader, "get_source"):
  176. source = loader.get_source(module_name)
  177. if source is not None:
  178. source = source.splitlines()
  179. if source is None:
  180. try:
  181. f = open(filename)
  182. try:
  183. source = f.readlines()
  184. finally:
  185. f.close()
  186. except (OSError, IOError):
  187. pass
  188. if source is None:
  189. return None, [], None, []
  190. encoding = 'ascii'
  191. for line in source[:2]:
  192. # File coding may be specified. Match pattern from PEP-263
  193. # (http://www.python.org/dev/peps/pep-0263/)
  194. match = re.search(r'coding[:=]\s*([-\w.]+)', line)
  195. if match:
  196. encoding = match.group(1)
  197. break
  198. source = [unicode(sline, encoding, 'replace') for sline in source]
  199. lower_bound = max(0, lineno - context_lines)
  200. upper_bound = lineno + context_lines
  201. pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
  202. context_line = source[lineno].strip('\n')
  203. post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]
  204. return lower_bound, pre_context, context_line, post_context
  205. def get_traceback_frames(self):
  206. frames = []
  207. tb = self.tb
  208. while tb is not None:
  209. # support for __traceback_hide__ which is used by a few libraries
  210. # to hide internal frames.
  211. if tb.tb_frame.f_locals.get('__traceback_hide__'):
  212. tb = tb.tb_next
  213. continue
  214. filename = tb.tb_frame.f_code.co_filename
  215. function = tb.tb_frame.f_code.co_name
  216. lineno = tb.tb_lineno - 1
  217. loader = tb.tb_frame.f_globals.get('__loader__')
  218. module_name = tb.tb_frame.f_globals.get('__name__')
  219. pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)
  220. if pre_context_lineno is not None:
  221. frames.append({
  222. 'tb': tb,
  223. 'filename': filename,
  224. 'function': function,
  225. 'lineno': lineno + 1,
  226. 'vars': tb.tb_frame.f_locals.items(),
  227. 'id': id(tb),
  228. 'pre_context': pre_context,
  229. 'context_line': context_line,
  230. 'post_context': post_context,
  231. 'pre_context_lineno': pre_context_lineno + 1,
  232. })
  233. tb = tb.tb_next
  234. return frames
  235. def format_exception(self):
  236. """
  237. Return the same data as from traceback.format_exception.
  238. """
  239. import traceback
  240. frames = self.get_traceback_frames()
  241. tb = [ (f['filename'], f['lineno'], f['function'], f['context_line']) for f in frames ]
  242. list = ['Traceback (most recent call last):\n']
  243. list += traceback.format_list(tb)
  244. list += traceback.format_exception_only(self.exc_type, self.exc_value)
  245. return list
  246. def technical_404_response(request, exception):
  247. "Create a technical 404 error response. The exception should be the Http404."
  248. try:
  249. tried = exception.args[0]['tried']
  250. except (IndexError, TypeError, KeyError):
  251. tried = []
  252. else:
  253. if not tried:
  254. # tried exists but is an empty list. The URLconf must've been empty.
  255. return empty_urlconf(request)
  256. urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
  257. if isinstance(urlconf, types.ModuleType):
  258. urlconf = urlconf.__name__
  259. t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 template')
  260. c = Context({
  261. 'urlconf': urlconf,
  262. 'root_urlconf': settings.ROOT_URLCONF,
  263. 'request_path': request.path_info[1:], # Trim leading slash
  264. 'urlpatterns': tried,
  265. 'reason': smart_str(exception, errors='replace'),
  266. 'request': request,
  267. 'settings': get_safe_settings(),
  268. })
  269. return HttpResponseNotFound(t.render(c), mimetype='text/html')
  270. def empty_urlconf(request):
  271. "Create an empty URLconf 404 error response."
  272. t = Template(EMPTY_URLCONF_TEMPLATE, name='Empty URLConf template')
  273. c = Context({
  274. 'project_name': settings.SETTINGS_MODULE.split('.')[0]
  275. })
  276. return HttpResponse(t.render(c), mimetype='text/html')
  277. #
  278. # Templates are embedded in the file so that we know the error handler will
  279. # always work even if the template loader is broken.
  280. #
  281. TECHNICAL_500_TEMPLATE = """
  282. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  283. <html lang="en">
  284. <head>
  285. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  286. <meta name="robots" content="NONE,NOARCHIVE">
  287. <title>{% if exception_type %}{{ exception_type }}{% else %}Report{% endif %}{% if request %} at {{ request.path_info|escape }}{% endif %}</title>
  288. <style type="text/css">
  289. html * { padding:0; margin:0; }
  290. body * { padding:10px 20px; }
  291. body * * { padding:0; }
  292. body { font:small sans-serif; }
  293. body>div { border-bottom:1px solid #ddd; }
  294. h1 { font-weight:normal; }
  295. h2 { margin-bottom:.8em; }
  296. h2 span { font-size:80%; color:#666; font-weight:normal; }
  297. h3 { margin:1em 0 .5em 0; }
  298. h4 { margin:0 0 .5em 0; font-weight: normal; }
  299. code, pre { font-size: 100%; }
  300. table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
  301. tbody td, tbody th { vertical-align:top; padding:2px 3px; }
  302. thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
  303. tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  304. table.vars { margin:5px 0 2px 40px; }
  305. table.vars td, table.req td { font-family:monospace; }
  306. table td.code { width:100%; }
  307. table td.code pre { overflow:hidden; }
  308. table.source th { color:#666; }
  309. table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
  310. ul.traceback { list-style-type:none; }
  311. ul.traceback li.frame { padding-bottom:1em; }
  312. div.context { padding:10px 0; overflow:hidden; }
  313. div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
  314. div.context ol li { font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
  315. div.context ol li pre { display:inline; }
  316. div.context ol.context-line li { color:black; background-color:#ccc; }
  317. div.context ol.context-line li span { position:absolute; right:32px; }
  318. div.commands { margin-left: 40px; }
  319. div.commands a { color:black; text-decoration:none; }
  320. #summary { background: #ffc; }
  321. #summary h2 { font-weight: normal; color: #666; }
  322. #explanation { background:#eee; }
  323. #template, #template-not-exist { background:#f6f6f6; }
  324. #template-not-exist ul { margin: 0 0 0 20px; }
  325. #unicode-hint { background:#eee; }
  326. #traceback { background:#eee; }
  327. #requestinfo { background:#f6f6f6; padding-left:120px; }
  328. #summary table { border:none; background:transparent; }
  329. #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
  330. #requestinfo h3 { margin-bottom:-1em; }
  331. .error { background: #ffc; }
  332. .specific { color:#cc3300; font-weight:bold; }
  333. h2 span.commands { font-size:.7em;}
  334. span.commands a:link {color:#5E5694;}
  335. pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
  336. </style>
  337. {% if not is_email %}
  338. <script type="text/javascript">
  339. //<!--
  340. function getElementsByClassName(oElm, strTagName, strClassName){
  341. // Written by Jonathan Snook, http://www.snook.ca/jon; Add-ons by Robert Nyman, http://www.robertnyman.com
  342. var arrElements = (strTagName == "*" && document.all)? document.all :
  343. oElm.getElementsByTagName(strTagName);
  344. var arrReturnElements = new Array();
  345. strClassName = strClassName.replace(/\-/g, "\\-");
  346. var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  347. var oElement;
  348. for(var i=0; i<arrElements.length; i++){
  349. oElement = arrElements[i];
  350. if(oRegExp.test(oElement.className)){
  351. arrReturnElements.push(oElement);
  352. }
  353. }
  354. return (arrReturnElements)
  355. }
  356. function hideAll(elems) {
  357. for (var e = 0; e < elems.length; e++) {
  358. elems[e].style.display = 'none';
  359. }
  360. }
  361. window.onload = function() {
  362. hideAll(getElementsByClassName(document, 'table', 'vars'));
  363. hideAll(getElementsByClassName(document, 'ol', 'pre-context'));
  364. hideAll(getElementsByClassName(document, 'ol', 'post-context'));
  365. hideAll(getElementsByClassName(document, 'div', 'pastebin'));
  366. }
  367. function toggle() {
  368. for (var i = 0; i < arguments.length; i++) {
  369. var e = document.getElementById(arguments[i]);
  370. if (e) {
  371. e.style.display = e.style.display == 'none' ? 'block' : 'none';
  372. }
  373. }
  374. return false;
  375. }
  376. function varToggle(link, id) {
  377. toggle('v' + id);
  378. var s = link.getElementsByTagName('span')[0];
  379. var uarr = String.fromCharCode(0x25b6);
  380. var darr = String.fromCharCode(0x25bc);
  381. s.innerHTML = s.innerHTML == uarr ? darr : uarr;
  382. return false;
  383. }
  384. function switchPastebinFriendly(link) {
  385. s1 = "Switch to copy-and-paste view";
  386. s2 = "Switch back to interactive view";
  387. link.innerHTML = link.innerHTML == s1 ? s2 : s1;
  388. toggle('browserTraceback', 'pastebinTraceback');
  389. return false;
  390. }
  391. //-->
  392. </script>
  393. {% endif %}
  394. </head>
  395. <body>
  396. <div id="summary">
  397. <h1>{% if exception_type %}{{ exception_type }}{% else %}Report{% endif %}{% if request %} at {{ request.path_info|escape }}{% endif %}</h1>
  398. <pre class="exception_value">{% if exception_value %}{{ exception_value|force_escape }}{% else %}No exception supplied{% endif %}</pre>
  399. <table class="meta">
  400. {% if request %}
  401. <tr>
  402. <th>Request Method:</th>
  403. <td>{{ request.META.REQUEST_METHOD }}</td>
  404. </tr>
  405. <tr>
  406. <th>Request URL:</th>
  407. <td>{{ request.build_absolute_uri|escape }}</td>
  408. </tr>
  409. {% endif %}
  410. <tr>
  411. <th>Django Version:</th>
  412. <td>{{ django_version_info }}</td>
  413. </tr>
  414. {% if exception_type %}
  415. <tr>
  416. <th>Exception Type:</th>
  417. <td>{{ exception_type }}</td>
  418. </tr>
  419. {% endif %}
  420. {% if exception_type and exception_value %}
  421. <tr>
  422. <th>Exception Value:</th>
  423. <td><pre>{{ exception_value|force_escape }}</pre></td>
  424. </tr>
  425. {% endif %}
  426. {% if lastframe %}
  427. <tr>
  428. <th>Exception Location:</th>
  429. <td>{{ lastframe.filename|escape }} in {{ lastframe.function|escape }}, line {{ lastframe.lineno }}</td>
  430. </tr>
  431. {% endif %}
  432. <tr>
  433. <th>Python Executable:</th>
  434. <td>{{ sys_executable|escape }}</td>
  435. </tr>
  436. <tr>
  437. <th>Python Version:</th>
  438. <td>{{ sys_version_info }}</td>
  439. </tr>
  440. <tr>
  441. <th>Python Path:</th>
  442. <td><pre>{{ sys_path|pprint }}</pre></td>
  443. </tr>
  444. <tr>
  445. <th>Server time:</th>
  446. <td>{{server_time|date:"r"}}</td>
  447. </tr>
  448. </table>
  449. </div>
  450. {% if unicode_hint %}
  451. <div id="unicode-hint">
  452. <h2>Unicode error hint</h2>
  453. <p>The string that could not be encoded/decoded was: <strong>{{ unicode_hint|force_escape }}</strong></p>
  454. </div>
  455. {% endif %}
  456. {% if template_does_not_exist %}
  457. <div id="template-not-exist">
  458. <h2>Template-loader postmortem</h2>
  459. {% if loader_debug_info %}
  460. <p>Django tried loading these templates, in this order:</p>
  461. <ul>
  462. {% for loader in loader_debug_info %}
  463. <li>Using loader <code>{{ loader.loader }}</code>:
  464. <ul>{% for t in loader.templates %}<li><code>{{ t.name }}</code> (File {% if t.exists %}exists{% else %}does not exist{% endif %})</li>{% endfor %}</ul>
  465. </li>
  466. {% endfor %}
  467. </ul>
  468. {% else %}
  469. <p>Django couldn't find any templates because your <code>TEMPLATE_LOADERS</code> setting is empty!</p>
  470. {% endif %}
  471. </div>
  472. {% endif %}
  473. {% if template_info %}
  474. <div id="template">
  475. <h2>Template error</h2>
  476. <p>In template <code>{{ template_info.name }}</code>, error at line <strong>{{ template_info.line }}</strong></p>
  477. <h3>{{ template_info.message }}</h3>
  478. <table class="source{% if template_info.top %} cut-top{% endif %}{% ifnotequal template_info.bottom template_info.total %} cut-bottom{% endifnotequal %}">
  479. {% for source_line in template_info.source_lines %}
  480. {% ifequal source_line.0 template_info.line %}
  481. <tr class="error"><th>{{ source_line.0 }}</th>
  482. <td>{{ template_info.before }}<span class="specific">{{ template_info.during }}</span>{{ template_info.after }}</td></tr>
  483. {% else %}
  484. <tr><th>{{ source_line.0 }}</th>
  485. <td>{{ source_line.1 }}</td></tr>
  486. {% endifequal %}
  487. {% endfor %}
  488. </table>
  489. </div>
  490. {% endif %}
  491. {% if frames %}
  492. <div id="traceback">
  493. <h2>Traceback <span class="commands">{% if not is_email %}<a href="#" onclick="return switchPastebinFriendly(this);">Switch to copy-and-paste view</a></span>{% endif %}</h2>
  494. {% autoescape off %}
  495. <div id="browserTraceback">
  496. <ul class="traceback">
  497. {% for frame in frames %}
  498. <li class="frame">
  499. <code>{{ frame.filename|escape }}</code> in <code>{{ frame.function|escape }}</code>
  500. {% if frame.context_line %}
  501. <div class="context" id="c{{ frame.id }}">
  502. {% if frame.pre_context and not is_email %}
  503. <ol start="{{ frame.pre_context_lineno }}" class="pre-context" id="pre{{ frame.id }}">{% for line in frame.pre_context %}<li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')"><pre>{{ line|escape }}</pre></li>{% endfor %}</ol>
  504. {% endif %}
  505. <ol start="{{ frame.lineno }}" class="context-line"><li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')"><pre>{{ frame.context_line|escape }}</pre>{% if not is_email %} <span>...</span>{% endif %}</li></ol>
  506. {% if frame.post_context and not is_email %}
  507. <ol start='{{ frame.lineno|add:"1" }}' class="post-context" id="post{{ frame.id }}">{% for line in frame.post_context %}<li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')"><pre>{{ line|escape }}</pre></li>{% endfor %}</ol>
  508. {% endif %}
  509. </div>
  510. {% endif %}
  511. {% if frame.vars %}
  512. <div class="commands">
  513. {% if is_email %}
  514. <h2>Local Vars</h2>
  515. {% else %}
  516. <a href="#" onclick="return varToggle(this, '{{ frame.id }}')"><span>&#x25b6;</span> Local vars</a>
  517. {% endif %}
  518. </div>
  519. <table class="vars" id="v{{ frame.id }}">
  520. <thead>
  521. <tr>
  522. <th>Variable</th>
  523. <th>Value</th>
  524. </tr>
  525. </thead>
  526. <tbody>
  527. {% for var in frame.vars|dictsort:"0" %}
  528. <tr>
  529. <td>{{ var.0|force_escape }}</td>
  530. <td class="code"><pre>{{ var.1 }}</pre></td>
  531. </tr>
  532. {% endfor %}
  533. </tbody>
  534. </table>
  535. {% endif %}
  536. </li>
  537. {% endfor %}
  538. </ul>
  539. </div>
  540. {% endautoescape %}
  541. <form action="http://dpaste.com/" name="pasteform" id="pasteform" method="post">
  542. {% if not is_email %}
  543. <div id="pastebinTraceback" class="pastebin">
  544. <input type="hidden" name="language" value="PythonConsole">
  545. <input type="hidden" name="title" value="{{ exception_type|escape }}{% if request %} at {{ request.path_info|escape }}{% endif %}">
  546. <input type="hidden" name="source" value="Django Dpaste Agent">
  547. <input type="hidden" name="poster" value="Django">
  548. <textarea name="content" id="traceback_area" cols="140" rows="25">
  549. Environment:
  550. {% if request %}
  551. Request Method: {{ request.META.REQUEST_METHOD }}
  552. Request URL: {{ request.build_absolute_uri|escape }}
  553. {% endif %}
  554. Django Version: {{ django_version_info }}
  555. Python Version: {{ sys_version_info }}
  556. Installed Applications:
  557. {{ settings.INSTALLED_APPS|pprint }}
  558. Installed Middleware:
  559. {{ settings.MIDDLEWARE_CLASSES|pprint }}
  560. {% if template_does_not_exist %}Template Loader Error:
  561. {% if loader_debug_info %}Django tried loading these templates, in this order:
  562. {% for loader in loader_debug_info %}Using loader {{ loader.loader }}:
  563. {% for t in loader.templates %}{{ t.name }} (File {% if t.exists %}exists{% else %}does not exist{% endif %})
  564. {% endfor %}{% endfor %}
  565. {% else %}Django couldn't find any templates because your TEMPLATE_LOADERS setting is empty!
  566. {% endif %}
  567. {% endif %}{% if template_info %}
  568. Template error:
  569. In template {{ template_info.name }}, error at line {{ template_info.line }}
  570. {{ template_info.message }}{% for source_line in template_info.source_lines %}{% ifequal source_line.0 template_info.line %}
  571. {{ source_line.0 }} : {{ template_info.before }} {{ template_info.during }} {{ template_info.after }}
  572. {% else %}
  573. {{ source_line.0 }} : {{ source_line.1 }}
  574. {% endifequal %}{% endfor %}{% endif %}
  575. Traceback:
  576. {% for frame in frames %}File "{{ frame.filename|escape }}" in {{ frame.function|escape }}
  577. {% if frame.context_line %} {{ frame.lineno }}. {{ frame.context_line|escape }}{% endif %}
  578. {% endfor %}
  579. Exception Type: {{ exception_type|escape }}{% if request %} at {{ request.path_info|escape }}{% endif %}
  580. Exception Value: {{ exception_value|force_escape }}
  581. </textarea>
  582. <br><br>
  583. <input type="submit" value="Share this traceback on a public Web site">
  584. </div>
  585. </form>
  586. </div>
  587. {% endif %}
  588. {% endif %}
  589. <div id="requestinfo">
  590. <h2>Request information</h2>
  591. {% if request %}
  592. <h3 id="get-info">GET</h3>
  593. {% if request.GET %}
  594. <table class="req">
  595. <thead>
  596. <tr>
  597. <th>Variable</th>
  598. <th>Value</th>
  599. </tr>
  600. </thead>
  601. <tbody>
  602. {% for var in request.GET.items %}
  603. <tr>
  604. <td>{{ var.0 }}</td>
  605. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  606. </tr>
  607. {% endfor %}
  608. </tbody>
  609. </table>
  610. {% else %}
  611. <p>No GET data</p>
  612. {% endif %}
  613. <h3 id="post-info">POST</h3>
  614. {% if request.POST %}
  615. <table class="req">
  616. <thead>
  617. <tr>
  618. <th>Variable</th>
  619. <th>Value</th>
  620. </tr>
  621. </thead>
  622. <tbody>
  623. {% for var in request.POST.items %}
  624. <tr>
  625. <td>{{ var.0 }}</td>
  626. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  627. </tr>
  628. {% endfor %}
  629. </tbody>
  630. </table>
  631. {% else %}
  632. <p>No POST data</p>
  633. {% endif %}
  634. <h3 id="files-info">FILES</h3>
  635. {% if request.FILES %}
  636. <table class="req">
  637. <thead>
  638. <tr>
  639. <th>Variable</th>
  640. <th>Value</th>
  641. </tr>
  642. </thead>
  643. <tbody>
  644. {% for var in request.FILES.items %}
  645. <tr>
  646. <td>{{ var.0 }}</td>
  647. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  648. </tr>
  649. {% endfor %}
  650. </tbody>
  651. </table>
  652. {% else %}
  653. <p>No FILES data</p>
  654. {% endif %}
  655. <h3 id="cookie-info">COOKIES</h3>
  656. {% if request.COOKIES %}
  657. <table class="req">
  658. <thead>
  659. <tr>
  660. <th>Variable</th>
  661. <th>Value</th>
  662. </tr>
  663. </thead>
  664. <tbody>
  665. {% for var in request.COOKIES.items %}
  666. <tr>
  667. <td>{{ var.0 }}</td>
  668. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  669. </tr>
  670. {% endfor %}
  671. </tbody>
  672. </table>
  673. {% else %}
  674. <p>No cookie data</p>
  675. {% endif %}
  676. <h3 id="meta-info">META</h3>
  677. <table class="req">
  678. <thead>
  679. <tr>
  680. <th>Variable</th>
  681. <th>Value</th>
  682. </tr>
  683. </thead>
  684. <tbody>
  685. {% for var in request.META.items|dictsort:"0" %}
  686. <tr>
  687. <td>{{ var.0 }}</td>
  688. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  689. </tr>
  690. {% endfor %}
  691. </tbody>
  692. </table>
  693. {% else %}
  694. <p>Request data not supplied</p>
  695. {% endif %}
  696. <h3 id="settings-info">Settings</h3>
  697. <h4>Using settings module <code>{{ settings.SETTINGS_MODULE }}</code></h4>
  698. <table class="req">
  699. <thead>
  700. <tr>
  701. <th>Setting</th>
  702. <th>Value</th>
  703. </tr>
  704. </thead>
  705. <tbody>
  706. {% for var in settings.items|dictsort:"0" %}
  707. <tr>
  708. <td>{{ var.0 }}</td>
  709. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  710. </tr>
  711. {% endfor %}
  712. </tbody>
  713. </table>
  714. </div>
  715. {% if not is_email %}
  716. <div id="explanation">
  717. <p>
  718. You're seeing this error because you have <code>DEBUG = True</code> in your
  719. Django settings file. Change that to <code>False</code>, and Django will
  720. display a standard 500 page.
  721. </p>
  722. </div>
  723. {% endif %}
  724. </body>
  725. </html>
  726. """
  727. TECHNICAL_404_TEMPLATE = """
  728. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  729. <html lang="en">
  730. <head>
  731. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  732. <title>Page not found at {{ request.path_info|escape }}</title>
  733. <meta name="robots" content="NONE,NOARCHIVE">
  734. <style type="text/css">
  735. html * { padding:0; margin:0; }
  736. body * { padding:10px 20px; }
  737. body * * { padding:0; }
  738. body { font:small sans-serif; background:#eee; }
  739. body>div { border-bottom:1px solid #ddd; }
  740. h1 { font-weight:normal; margin-bottom:.4em; }
  741. h1 span { font-size:60%; color:#666; font-weight:normal; }
  742. table { border:none; border-collapse: collapse; width:100%; }
  743. td, th { vertical-align:top; padding:2px 3px; }
  744. th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  745. #info { background:#f6f6f6; }
  746. #info ol { margin: 0.5em 4em; }
  747. #info ol li { font-family: monospace; }
  748. #summary { background: #ffc; }
  749. #explanation { background:#eee; border-bottom: 0px none; }
  750. </style>
  751. </head>
  752. <body>
  753. <div id="summary">
  754. <h1>Page not found <span>(404)</span></h1>
  755. <table class="meta">
  756. <tr>
  757. <th>Request Method:</th>
  758. <td>{{ request.META.REQUEST_METHOD }}</td>
  759. </tr>
  760. <tr>
  761. <th>Request URL:</th>
  762. <td>{{ request.build_absolute_uri|escape }}</td>
  763. </tr>
  764. </table>
  765. </div>
  766. <div id="info">
  767. {% if urlpatterns %}
  768. <p>
  769. Using the URLconf defined in <code>{{ urlconf }}</code>,
  770. Django tried these URL patterns, in this order:
  771. </p>
  772. <ol>
  773. {% for pattern in urlpatterns %}
  774. <li>
  775. {% for pat in pattern %}
  776. {{ pat.regex.pattern }}
  777. {% if forloop.last and pat.name %}[name='{{ pat.name }}']{% endif %}
  778. {% endfor %}
  779. </li>
  780. {% endfor %}
  781. </ol>
  782. <p>The current URL, <code>{{ request_path|escape }}</code>, didn't match any of these.</p>
  783. {% else %}
  784. <p>{{ reason }}</p>
  785. {% endif %}
  786. </div>
  787. <div id="explanation">
  788. <p>
  789. You're seeing this error because you have <code>DEBUG = True</code> in
  790. your Django settings file. Change that to <code>False</code>, and Django
  791. will display a standard 404 page.
  792. </p>
  793. </div>
  794. </body>
  795. </html>
  796. """
  797. EMPTY_URLCONF_TEMPLATE = """
  798. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  799. <html lang="en"><head>
  800. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  801. <meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title>
  802. <style type="text/css">
  803. html * { padding:0; margin:0; }
  804. body * { padding:10px 20px; }
  805. body * * { padding:0; }
  806. body { font:small sans-serif; }
  807. body>div { border-bottom:1px solid #ddd; }
  808. h1 { font-weight:normal; }
  809. h2 { margin-bottom:.8em; }
  810. h2 span { font-size:80%; color:#666; font-weight:normal; }
  811. h3 { margin:1em 0 .5em 0; }
  812. h4 { margin:0 0 .5em 0; font-weight: normal; }
  813. table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
  814. tbody td, tbody th { vertical-align:top; padding:2px 3px; }
  815. thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
  816. tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  817. ul { margin-left: 2em; margin-top: 1em; }
  818. #summary { background: #e0ebff; }
  819. #summary h2 { font-weight: normal; color: #666; }
  820. #explanation { background:#eee; }
  821. #instructions { background:#f6f6f6; }
  822. #summary table { border:none; background:transparent; }
  823. </style>
  824. </head>
  825. <body>
  826. <div id="summary">
  827. <h1>It worked!</h1>
  828. <h2>Congratulations on your first Django-powered page.</h2>
  829. </div>
  830. <div id="instructions">
  831. <p>Of course, you haven't actually done any work yet. Here's what to do next:</p>
  832. <ul>
  833. <li>If you plan to use a database, edit the <code>DATABASES</code> setting in <code>{{ project_name }}/settings.py</code>.</li>
  834. <li>Start your first app by running <code>python {{ project_name }}/manage.py startapp [appname]</code>.</li>
  835. </ul>
  836. </div>
  837. <div id="explanation">
  838. <p>
  839. You're seeing this message because you have <code>DEBUG = True</code> in your
  840. Django settings file and you haven't configured any URLs. Get to work!
  841. </p>
  842. </div>
  843. </body></html>
  844. """