Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #333 and #440 -- Split DEFAULT_MIME_TYPE setting into DEFAULT_C…

…ONTENT_TYPE and DEFAULT_CHARSET. Thanks, Maniac.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@786 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ab9aacd4db5d1e69ff2c78bae69fcabe5252d395 1 parent 261ab16
Adrian Holovaty authored October 06, 2005
8  django/conf/global_settings.py
@@ -32,9 +32,11 @@
32 32
 # notifications and other various e-mails.
33 33
 MANAGERS = ADMINS
34 34
 
35  
-# Default MIME type to use for all HttpResponse objects, if a MIME type
36  
-# isn't manually specified. This is directly used as the Content-Type header.
37  
-DEFAULT_MIME_TYPE = 'text/html; charset=utf-8'
  35
+# Default content type and charset to use for all HttpResponse objects, if a
  36
+# MIME type isn't manually specified. These are used to construct the
  37
+# Content-Type header.
  38
+DEFAULT_CONTENT_TYPE = 'text/html'
  39
+DEFAULT_CHARSET = 'utf-8'
38 40
 
39 41
 # E-mail address that error messages come from.
40 42
 SERVER_EMAIL = 'root@localhost'
7  django/core/formfields.py
... ...
@@ -1,6 +1,7 @@
1 1
 from django.core import validators
2 2
 from django.core.exceptions import PermissionDenied
3 3
 from django.utils.html import escape
  4
+from django.conf.settings import DEFAULT_CHARSET
4 5
 
5 6
 FORM_FIELD_ID_PREFIX = 'id_'
6 7
 
@@ -221,7 +222,7 @@ def __init__(self, field_name, length=30, maxlength=None, is_required=False, val
221 222
         self.validator_list = [self.isValidLength, self.hasNoNewlines] + validator_list
222 223
 
223 224
     def isValidLength(self, data, form):
224  
-        if data and self.maxlength and len(data) > self.maxlength:
  225
+        if data and self.maxlength and len(data.decode(DEFAULT_CHARSET)) > self.maxlength:
225 226
             raise validators.ValidationError, "Ensure your text is less than %s characters." % self.maxlength
226 227
 
227 228
     def hasNoNewlines(self, data, form):
@@ -235,7 +236,7 @@ def render(self, data):
235 236
         if self.maxlength:
236 237
             maxlength = 'maxlength="%s" ' % self.maxlength
237 238
         if isinstance(data, unicode):
238  
-            data = data.encode('utf-8')
  239
+            data = data.encode(DEFAULT_CHARSET)
239 240
         return '<input type="text" id="%s" class="v%s%s" name="%s" size="%s" value="%s" %s/>' % \
240 241
             (FORM_FIELD_ID_PREFIX + self.field_name, self.__class__.__name__, self.is_required and ' required' or '',
241 242
             self.field_name, self.length, escape(data), maxlength)
@@ -264,7 +265,7 @@ def render(self, data):
264 265
         if data is None:
265 266
             data = ''
266 267
         if isinstance(data, unicode):
267  
-            data = data.encode('utf-8')
  268
+            data = data.encode(DEFAULT_CHARSET)
268 269
         return '<textarea id="%s" class="v%s%s" name="%s" rows="%s" cols="%s">%s</textarea>' % \
269 270
             (FORM_FIELD_ID_PREFIX + self.field_name, self.__class__.__name__, self.is_required and ' required' or '',
270 271
             self.field_name, self.rows, self.cols, escape(data))
5  django/core/handlers/modpython.py
@@ -150,14 +150,15 @@ def __call__(self, req):
150 150
 
151 151
 def populate_apache_request(http_response, mod_python_req):
152 152
     "Populates the mod_python request object with an HttpResponse"
153  
-    mod_python_req.content_type = http_response['Content-Type'] or httpwrappers.DEFAULT_MIME_TYPE
  153
+    from django.conf import settings
  154
+    mod_python_req.content_type = http_response['Content-Type']
154 155
     for key, value in http_response.headers.items():
155 156
         if key != 'Content-Type':
156 157
             mod_python_req.headers_out[key] = value
157 158
     for c in http_response.cookies.values():
158 159
         mod_python_req.headers_out.add('Set-Cookie', c.output(header=''))
159 160
     mod_python_req.status = http_response.status_code
160  
-    mod_python_req.write(http_response.get_content_as_string('utf-8'))
  161
+    mod_python_req.write(http_response.get_content_as_string(settings.DEFAULT_CHARSET))
161 162
 
162 163
 def handler(req):
163 164
     # mod_python hooks into this function.
2  django/core/handlers/wsgi.py
@@ -167,6 +167,6 @@ def __call__(self, environ, start_response):
167 167
         response_headers = response.headers.items()
168 168
         for c in response.cookies.values():
169 169
             response_headers.append(('Set-Cookie', c.output(header='')))
170  
-        output = [response.get_content_as_string('utf-8')]
  170
+        output = [response.get_content_as_string(settings.DEFAULT_CHARSET)]
171 171
         start_response(status, response_headers)
172 172
         return output
3  django/core/template.py
@@ -55,6 +55,7 @@
55 55
 '\n<html>\n\n</html>\n'
56 56
 """
57 57
 import re
  58
+from django.conf.settings import DEFAULT_CHARSET
58 59
 
59 60
 __all__ = ('Template','Context','compile_string')
60 61
 
@@ -474,7 +475,7 @@ def render(self, context):
474 475
         if not isinstance(output, basestring):
475 476
             output = str(output)
476 477
         elif isinstance(output, unicode):
477  
-            output = output.encode('utf-8')
  478
+            output = output.encode(DEFAULT_CHARSET)
478 479
         return output
479 480
 
480 481
 def register_tag(token_command, callback_function):
2  django/middleware/cache.py
@@ -76,7 +76,7 @@ def process_response(self, request, response):
76 76
         Sets the cache, if needed.
77 77
         """
78 78
         if request._cache_middleware_set_cache:
79  
-            content = response.get_content_as_string('utf-8')
  79
+            content = response.get_content_as_string(settings.DEFAULT_CHARSET)
80 80
             if request._cache_middleware_accepts_gzip:
81 81
                 content = compress_string(content)
82 82
                 response.content = content
2  django/middleware/common.py
@@ -76,7 +76,7 @@ def process_response(self, request, response):
76 76
 
77 77
         # Use ETags, if requested.
78 78
         if settings.USE_ETAGS:
79  
-            etag = md5.new(response.get_content_as_string('utf-8')).hexdigest()
  79
+            etag = md5.new(response.get_content_as_string(settings.DEFAULT_CHARSET)).hexdigest()
80 80
             if request.META.get('HTTP_IF_NONE_MATCH') == etag:
81 81
                 response = httpwrappers.HttpResponseNotModified()
82 82
             else:
6  django/utils/httpwrappers.py
... ...
@@ -1,7 +1,7 @@
1 1
 from Cookie import SimpleCookie
2 2
 from pprint import pformat
3 3
 from urllib import urlencode
4  
-import datastructures
  4
+from django.utils import datastructures
5 5
 
6 6
 class HttpRequest(object): # needs to be new-style class because subclasses define "property"s
7 7
     "A basic HTTP request"
@@ -139,8 +139,8 @@ class HttpResponse:
139 139
     "A basic HTTP response, with content and dictionary-accessed headers"
140 140
     def __init__(self, content='', mimetype=None):
141 141
         if not mimetype:
142  
-            from django.conf.settings import DEFAULT_MIME_TYPE
143  
-            mimetype = DEFAULT_MIME_TYPE
  142
+            from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET
  143
+            mimetype = "%s; charset=%s" % (DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET)
144 144
         self.content = content
145 145
         self.headers = {'Content-Type':mimetype}
146 146
         self.cookies = SimpleCookie()
3  django/views/decorators/cache.py
... ...
@@ -1,6 +1,7 @@
1 1
 from django.core.cache import cache
2 2
 from django.utils.httpwrappers import HttpResponseNotModified
3 3
 from django.utils.text import compress_string
  4
+from django.conf.settings import DEFAULT_CHARSET
4 5
 import datetime, md5
5 6
 
6 7
 def cache_page(view_func, cache_timeout, key_prefix=''):
@@ -25,7 +26,7 @@ def _check_cache(request, *args, **kwargs):
25 26
         response = cache.get(cache_key, None)
26 27
         if response is None:
27 28
             response = view_func(request, *args, **kwargs)
28  
-            content = response.get_content_as_string('utf-8')
  29
+            content = response.get_content_as_string(DEFAULT_CHARSET)
29 30
             if accepts_gzip:
30 31
                 content = compress_string(content)
31 32
                 response.content = content

0 notes on commit ab9aacd

Please sign in to comment.
Something went wrong with that request. Please try again.