Opened 8 years ago

Closed 8 years ago

#27171 closed Cleanup/optimization (fixed)

#7201 Change 'unicode_literals' in TemplateCommand, change admin and view templates

Reported by: Ivan Owned by: nobody
Component: Template system Version: 1.10
Severity: Normal Keywords: admin_scripts, TemplateCommand
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you run manage.py startapp with python2, TemplateCommand context add from __future__ import unicode literals at the top.
If you using non askii symbols on python2, like cyrillic alphabet, you still have an error 'SyntaxError: Non-ASCII character' because encoding not specified.
Also admin and views templates needed unicode_literals too. For example, when you put some comments in non askii or raise error message.
Here is changes of TemplateCommand Context:

index 8c38f3f..b2ff964 100644
--- a/django/conf/app_template/admin.py-tpl
+++ b/django/conf/app_template/admin.py-tpl
@@ -1,3 +1,3 @@
-from django.contrib import admin
+{{ unicode_literals }}from django.contrib import admin
 
 # Register your models here.
diff --git a/django/conf/app_template/views.py-tpl b/django/conf/app_template/views.py-tpl
index 91ea44a..61821e7 100644
--- a/django/conf/app_template/views.py-tpl
+++ b/django/conf/app_template/views.py-tpl
@@ -1,3 +1,3 @@
-from django.shortcuts import render
+{{ unicode_literals }}from django.shortcuts import render
 
 # Create your views here.
diff --git a/django/core/management/templates.py b/django/core/management/templates.py
index df522e8..fd957fb 100644
--- a/django/core/management/templates.py
+++ b/django/core/management/templates.py
@@ -114,7 +114,8 @@ class TemplateCommand(BaseCommand):
             camel_case_name: camel_case_value,
             'docs_version': get_docs_version(),
             'django_version': django.__version__,
-            'unicode_literals': '' if six.PY3 else 'from __future__ import unicode_literals\n\n',
+            'unicode_literals': '' if six.PY3 else '# -*- coding: utf-8 -*-\n'
+                                                   'from __future__ import unicode_literals\n\n',
         }), autoescape=False)
 
         # Setup a stub settings environment for template rendering

Change History (1)

comment:1 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 9a2a5255:

Fixed #27171 -- Added unicode_literals and coding preamble to all files in startapp template on Python 2.

Note: See TracTickets for help on using tickets.
Back to Top