سؤال

مرحبًا ، أريد أن يكون لدي إصدار نص عادي من المحتوى الخاص بي. لذلك لدي قالب منفصل لذلك. أنا أتصل 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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top