Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Updated django/core/management/templates.py #245

Closed
wants to merge 2 commits into from

2 participants

@ghost

fix ticket 18091 using codecs.open as suggested in ticket discussion

@apollo13
Owner

Please see https://code.djangoproject.com/ticket/18091#comment:5 and reopen a new pull request once those issues are resolved.

@apollo13 apollo13 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 django/core/management/templates.py
@@ -8,6 +8,8 @@
import stat
import sys
import tempfile
+import codecs
+
try:
from urllib.request import urlretrieve
except ImportError: # Python 2
@@ -159,7 +161,7 @@ def handle(self, app_or_project, name, target=None, **options):
if filename.endswith(extensions) or filename in extra_files:
template = Template(content)
content = template.render(context)
- with open(new_path, 'w') as new_file:
+ with codecs.open(new_path, 'w', "utf-8") as new_file:
new_file.write(content)
if self.verbosity >= 2:
View
2  ...tests/admin_scripts/custom_templates/project_template/ticket-18091-non-ascii-template.txt
@@ -0,0 +1,2 @@
+Some non-ASCII text for testing ticket #18091:
+üäö €
View
13 tests/regressiontests/admin_scripts/tests.py
@@ -1572,3 +1572,16 @@ def test_custom_project_destination_missing(self):
self.assertOutput(err, "Destination directory '%s' does not exist, please create it first." % testproject_dir)
self.assertFalse(os.path.exists(testproject_dir))
+
+ def test_custom_project_template_with_non_ascii_templates(self):
+ "Ticket 18091: Make sure the startproject management command is able to render templates with non-ASCII content"
+ template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
+ args = ['startproject', '--template', template_path, '--extension=txt', 'customtestproject']
+ testproject_dir = os.path.join(test_dir, 'customtestproject')
+
+ out, err = self.run_django_admin(args)
+ self.addCleanup(shutil.rmtree, testproject_dir)
+ self.assertNoOutput(err)
+ self.assertTrue(os.path.isdir(testproject_dir))
+ self.assertEqual(open(os.path.join(testproject_dir, 'ticket-18091-non-ascii-template.txt')).read(),
+ 'Some non-ASCII text for testing ticket #18091:\n\xc3\xbc\xc3\xa4\xc3\xb6 \xe2\x82\xac\n')
Something went wrong with that request. Please try again.