0

I am trying to return a JSON response in Django that is called with Ajax. The JSON response will simply be a list with each index containing some sort of HTML code. I am running into some errors when I try to parse this response in JQuery after I have successfully pulled it with Ajax. The Chrome developer tools simply tell me this error when I try to log the received JSON response in the console Uncaught SyntaxError: Unexpected token &

Here is the code that I use to return a JSON response from Django that contains HTML code inside of it.

template = loader.get_template('home_post.html')
post_arr = []
for post in posts:
    context = Context({'image': post.image, 'body': post.body})
    post_arr.append(escape(template.render(context)))
return HttpResponse(json.dumps(post_arr), content_type='application/json')

Is there any way that I should be doing this other than what I am doing right now? As in, is there a built in way to escape HTML code so that I can put it in a JSON response?

1 Answer 1

1

Yes, use the built-in safe template filter.

Documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.