In Django, How do I get escaped html in HttpResponse?

后端 未结 3 1181
挽巷
挽巷 2021-01-11 15:55

The following code in one of my views returns unescaped html string which cannot be parsed in frontend since it is an Ajax request.

return render_to_response         


        
3条回答
  •  無奈伤痛
    2021-01-11 16:43

    To return just plain HTML to the client from within your view, use django.http.HttpResponse

    from django.http import HttpResponse
    
    def view(request)
        # Do stuff here
        output = '''
        
            
                Hey mum!
            
        '''
        return HttpResponse(output)
    

    To prevent the Django templating system from escaping HTML in a template, just use the |safe filter:

    response = ""
    
    # Meanwhile, in the template...
    
    {{response|safe}}

提交回复
热议问题