Skip to content

Commit a875f61

Browse files
mjtamlynapollo13
authored andcommitted
Fixed django#18634 -- Don't escape variables in the context for startproject/startapp.
The & symbols which can come up in the secret key were being escaped to &.
1 parent 59d9977 commit a875f61

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

django/core/management/templates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def handle(self, app_or_project, name, target=None, **options):
115115
context = Context(dict(options, **{
116116
base_name: name,
117117
base_directory: top_dir,
118-
}))
118+
}), autoescape=False)
119119

120120
# Setup a stub settings environment for template rendering
121121
from django.conf import settings
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# this file uses the {{ extra }} variable
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from optparse import make_option
2+
3+
from django.core.management.commands.startproject import Command as BaseCommand
4+
5+
6+
class Command(BaseCommand):
7+
option_list = BaseCommand.option_list + (
8+
make_option('--extra',
9+
action='store', dest='extra',
10+
help='An arbitrary extra value passed to the context'),
11+
)

tests/regressiontests/admin_scripts/tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,24 @@ def test_custom_project_template_context_variables(self):
15411541
self.assertIn("project_name = 'another_project'", content)
15421542
self.assertIn("project_directory = '%s'" % testproject_dir, content)
15431543

1544+
def test_no_escaping_of_project_variables(self):
1545+
"Make sure template context variables are not html escaped"
1546+
# We're using a custom command so we need the alternate settings
1547+
self.write_settings('alternate_settings.py')
1548+
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
1549+
args = ['custom_startproject', '--template', template_path, 'another_project', 'project_dir', '--extra', '<&>', '--settings=alternate_settings']
1550+
testproject_dir = os.path.join(test_dir, 'project_dir')
1551+
os.mkdir(testproject_dir)
1552+
out, err = self.run_manage(args)
1553+
self.addCleanup(shutil.rmtree, testproject_dir)
1554+
self.assertNoOutput(err)
1555+
test_manage_py = os.path.join(testproject_dir, 'additional_dir', 'extra.py')
1556+
with open(test_manage_py, 'r') as fp:
1557+
content = fp.read()
1558+
self.assertIn("<&>", content)
1559+
# tidy up alternate settings
1560+
self.remove_settings('alternate_settings.py')
1561+
15441562
def test_custom_project_destination_missing(self):
15451563
"""
15461564
Make sure an exception is raised when the provided

0 commit comments

Comments
 (0)