Domanda

Ciao voglio avere una versione in chiaro dei miei contenuti disponibili. Così ho un modello separato per questo. Sto chiamando render_to_response con mimetype="text/plain" ma voglio dire a un browser di aprire la pagina nel http-risposta che il contenuto è codifica UTF-8. Come faccio a farlo (ad esempio quello che devo aggiungere al render_to_response)?

È stato utile?

Soluzione

Basta aggiungere charset a mimetype in questo modo:

mimetype="text/html; charset=utf-8"

Che cosa succede veramente dietro scena è che mimetype è preso fuori kwargs in render_to_response.

httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

e inviato al HttpResponse che lo imposta come 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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top