mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #26402 -- Added relative path support in include/extends template tags.
This commit is contained in:
parent
ad403ffa45
commit
aec4f97555
20 changed files with 206 additions and 0 deletions
|
@ -0,0 +1 @@
|
|||
{% include "./../../three.html" %}
|
|
@ -0,0 +1 @@
|
|||
{% include "./include_content.html" %}
|
|
@ -0,0 +1 @@
|
|||
dir2 include
|
|
@ -0,0 +1,3 @@
|
|||
{% extends "./../../one.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} dir2 one{% endblock %}
|
3
tests/template_tests/relative_templates/dir1/looped.html
Normal file
3
tests/template_tests/relative_templates/dir1/looped.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends "./dir2/../looped.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} dir1 three{% endblock %}
|
3
tests/template_tests/relative_templates/dir1/one.html
Normal file
3
tests/template_tests/relative_templates/dir1/one.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends "./../one.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} dir1 one{% endblock %}
|
3
tests/template_tests/relative_templates/dir1/one1.html
Normal file
3
tests/template_tests/relative_templates/dir1/one1.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends './../one.html' %}
|
||||
|
||||
{% block content %}{{ block.super }} dir1 one{% endblock %}
|
3
tests/template_tests/relative_templates/dir1/one2.html
Normal file
3
tests/template_tests/relative_templates/dir1/one2.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends '../one.html' %}
|
||||
|
||||
{% block content %}{{ block.super }} dir1 one{% endblock %}
|
3
tests/template_tests/relative_templates/dir1/one3.html
Normal file
3
tests/template_tests/relative_templates/dir1/one3.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends "../one.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} dir1 one{% endblock %}
|
3
tests/template_tests/relative_templates/dir1/three.html
Normal file
3
tests/template_tests/relative_templates/dir1/three.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends "./dir2/../../three.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} dir1 three{% endblock %}
|
3
tests/template_tests/relative_templates/dir1/two.html
Normal file
3
tests/template_tests/relative_templates/dir1/two.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends "./dir2/one.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} dir1 two{% endblock %}
|
|
@ -0,0 +1,3 @@
|
|||
{% extends "./../two.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} one{% endblock %}
|
|
@ -0,0 +1 @@
|
|||
{% include "./../three.html" %}
|
3
tests/template_tests/relative_templates/one.html
Normal file
3
tests/template_tests/relative_templates/one.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends "./two.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} one{% endblock %}
|
1
tests/template_tests/relative_templates/three.html
Normal file
1
tests/template_tests/relative_templates/three.html
Normal file
|
@ -0,0 +1 @@
|
|||
{% block content %}three{% endblock %}
|
3
tests/template_tests/relative_templates/two.html
Normal file
3
tests/template_tests/relative_templates/two.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% extends "./three.html" %}
|
||||
|
||||
{% block content %}{{ block.super }} two{% endblock %}
|
105
tests/template_tests/test_extends_relative.py
Normal file
105
tests/template_tests/test_extends_relative.py
Normal file
|
@ -0,0 +1,105 @@
|
|||
import os
|
||||
|
||||
from django.template import Context, Engine, TemplateSyntaxError
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from .utils import ROOT
|
||||
|
||||
RELATIVE = os.path.join(ROOT, 'relative_templates')
|
||||
|
||||
|
||||
class ExtendsRelativeBehaviorTests(SimpleTestCase):
|
||||
|
||||
def test_normal_extend(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('one.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three two one')
|
||||
|
||||
def test_dir1_extend(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/one.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three two one dir1 one')
|
||||
|
||||
def test_dir1_extend1(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/one1.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three two one dir1 one')
|
||||
|
||||
def test_dir1_extend2(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/one2.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three two one dir1 one')
|
||||
|
||||
def test_dir1_extend3(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/one3.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three two one dir1 one')
|
||||
|
||||
def test_dir2_extend(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/dir2/one.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three two one dir2 one')
|
||||
|
||||
def test_extend_error(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
msg = (
|
||||
"The relative path '\"./../two.html\"' points outside the file "
|
||||
"hierarchy that template 'error_extends.html' is in."
|
||||
)
|
||||
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
||||
engine.render_to_string('error_extends.html')
|
||||
|
||||
|
||||
class IncludeRelativeBehaviorTests(SimpleTestCase):
|
||||
|
||||
def test_normal_include(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/dir2/inc2.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'dir2 include')
|
||||
|
||||
def test_dir2_include(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/dir2/inc1.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three')
|
||||
|
||||
def test_include_error(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
msg = (
|
||||
"The relative path '\"./../three.html\"' points outside the file "
|
||||
"hierarchy that template 'error_include.html' is in."
|
||||
)
|
||||
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
||||
engine.render_to_string('error_include.html')
|
||||
|
||||
|
||||
class ExtendsMixedBehaviorTests(SimpleTestCase):
|
||||
|
||||
def test_mixing1(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/two.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three two one dir2 one dir1 two')
|
||||
|
||||
def test_mixing2(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
template = engine.get_template('dir1/three.html')
|
||||
output = template.render(Context({}))
|
||||
self.assertEqual(output.strip(), 'three dir1 three')
|
||||
|
||||
def test_mixing_loop(self):
|
||||
engine = Engine(dirs=[RELATIVE])
|
||||
msg = (
|
||||
"The relative path '\"./dir2/../looped.html\"' was translated to "
|
||||
"template name \'dir1/looped.html\', the same template in which "
|
||||
"the tag appears."
|
||||
)
|
||||
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
||||
engine.render_to_string('dir1/looped.html')
|
Loading…
Add table
Add a link
Reference in a new issue