def technical_404_response(request, exception): "Create a technical 404 error response. The exception should be the Http404." try: tried = exception.args[0]['tried'] except (IndexError, TypeError, KeyError): tried = [] else: if not tried: # tried exists but is an empty list. The URLconf must've been empty. return empty_urlconf(request) urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF) if isinstance(urlconf, types.ModuleType): urlconf = urlconf.__name__ t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 template') c = Context({ 'urlconf': urlconf, 'root_urlconf': settings.ROOT_URLCONF, 'request_path': request.path_info[1:], # Trim leading slash 'urlpatterns': tried, 'reason': smart_str(exception, errors='replace'), 'request': request, 'settings': get_safe_settings(), }) return HttpResponseNotFound(t.render(c), mimetype='text/html')
def test_textile(self): t = Template("{% load markup %}{{ textile_content|textile }}") rendered = t.render(Context({'textile_content': self.textile_content})).strip() self.assertEqual( rendered.replace('\t', ''), """<p>Paragraph 1</p> <p>Paragraph 2 with “quotes” and <code>code</code></p>""")
def humanize_tester(self, test_list, result_list, method): # Using max below ensures we go through both lists # However, if the lists are not equal length, this raises an exception for test_content, result in zip(test_list, result_list): t = Template('{%% load humanize %%}{{ test_content|%s }}' % method) rendered = t.render(Context(locals())).strip() self.assertEqual( rendered, escape(result), msg="%s test failed, produced '%s', should've produced '%s'" % (method, rendered, result))
def csrf_failure(request, reason=""): """ Default view used when request fails CSRF protection """ from my_django.middleware.csrf import REASON_NO_REFERER t = Template(CSRF_FAILURE_TEMPLATE) c = Context({ 'DEBUG': settings.DEBUG, 'reason': reason, 'no_referer': reason == REASON_NO_REFERER }) return HttpResponseForbidden(t.render(c), mimetype='text/html')
def test_docutils(self): t = Template("{% load markup %}{{ rest_content|restructuredtext }}") rendered = t.render(Context({'rest_content': self.rest_content})).strip() # Different versions of docutils return slightly different HTML try: # Docutils v0.4 and earlier self.assertEqual( rendered, """<p>Paragraph 1</p> <p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>""" ) except AssertionError, e: # Docutils from SVN (which will become 0.5) self.assertEqual( rendered, """<p>Paragraph 1</p> <p>Paragraph 2 with a <a class="reference external" href="http://www.example.com/">link</a></p>""" )
def directory_index(path, fullpath): try: t = loader.select_template(['static/directory_index.html', 'static/directory_index']) except TemplateDoesNotExist: t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE, name='Default directory index template') files = [] for f in os.listdir(fullpath): if not f.startswith('.'): if os.path.isdir(os.path.join(fullpath, f)): f += '/' files.append(f) c = Context({ 'directory' : path + '/', 'file_list' : files, }) return HttpResponse(t.render(c))
def show(request): t = Template(TEMPLATE) return HttpResponse(t.render(RequestContext(request)))
def test_no_markdown(self): t = Template("{% load markup %}{{ markdown_content|markdown }}") rendered = t.render( Context({'markdown_content': self.markdown_content})).strip() self.assertEqual(rendered, self.markdown_content)
def test_markdown_attribute_enable(self): t = Template("{% load markup %}{{ markdown_content|markdown }}") markdown_content = "{@onclick=alert('hi')}some paragraph" rendered = t.render(Context({'markdown_content': markdown_content})).strip() self.assertFalse('@' in rendered)
def test_markdown(self): t = Template("{% load markup %}{{ markdown_content|markdown }}") rendered = t.render( Context({'markdown_content': self.markdown_content})).strip() pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""") self.assertTrue(pattern.match(rendered))
def test_no_textile(self): t = Template("{% load markup %}{{ textile_content|textile }}") rendered = t.render(Context({'textile_content': self.textile_content})).strip() self.assertEqual(rendered, escape(self.textile_content))
def remote_user_auth_view(request): "Dummy view for remote user tests" t = Template("Username is {{ user }}.") c = RequestContext(request, {}) return HttpResponse(t.render(c))
def test_no_docutils(self): t = Template("{% load markup %}{{ rest_content|restructuredtext }}") rendered = t.render(Context({'rest_content': self.rest_content})).strip() self.assertEqual(rendered, self.rest_content)
old_path = path.join(root, filename) new_path = path.join(top_dir, relative_dir, filename.replace(base_name, name)) if path.exists(new_path): raise CommandError("%s already exists, overlaying a " "project or app into an existing " "directory won't replace conflicting " "files" % new_path) # Only render the Python files, as we don't want to # accidentally render Django templates files with open(old_path, 'r') as template_file: content = template_file.read() if filename.endswith(extensions) or filename in extra_files: template = Template(content) content = template.render(context) with open(new_path, 'w') as new_file: new_file.write(content) if self.verbosity >= 2: self.stdout.write("Creating %s\n" % new_path) try: shutil.copymode(old_path, new_path) self.make_writeable(new_path) except OSError: notice = self.style.NOTICE( "Notice: Couldn't set permission bits on %s. You're " "probably using an uncommon filesystem setup. No " "problem.\n" % new_path) sys.stderr.write(smart_str(notice))
def empty_urlconf(request): "Create an empty URLconf 404 error response." t = Template(EMPTY_URLCONF_TEMPLATE, name='Empty URLConf template') c = Context({'project_name': settings.SETTINGS_MODULE.split('.')[0]}) return HttpResponse(t.render(c), mimetype='text/html')
def get_traceback_html(self): "Return HTML version of debug 500 HTTP error page." t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 template') c = Context(self.get_traceback_data()) return t.render(c)
def get_traceback_text(self): "Return plain text version of debug 500 HTTP error page." t = Template(TECHNICAL_500_TEXT_TEMPLATE, name='Technical 500 template') c = Context(self.get_traceback_data(), autoescape=False) return t.render(c)