إرسال رأس ترميز المحتوى في Django
-
26-09-2019 - |
سؤال
مرحبًا ، أريد أن يكون لدي إصدار نص عادي من المحتوى الخاص بي. لذلك لدي قالب منفصل لذلك. أنا أتصل render_to_response
مع mimetype="text/plain"
لكنني أريد أن أخبر متصفحًا يفتح تلك الصفحة في استجابة HTTP بأن المحتوى قد تم ترميزه في UTF-8. كيف أفعل ذلك (على سبيل المثال ، ماذا علي أن أضيف إليه render_to_response
)?
المحلول
فقط أضف charset إلى mimetype مثل هذا:
mimetype="text/html; charset=utf-8"
ما يحدث حقًا وراء الحادث هو أن Mimetype يتم إخراجها من Kwargs في render_to_response
.
httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
وأرسلت إلى HttpResponse
الذي يضعه على أنه content_type
:
if mimetype:
content_type = mimetype # For backwards compatibility
if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
settings.DEFAULT_CHARSET)
لا تنتمي إلى StackOverflow