[Answered ]-Django: Correct way to save HTML code inside database

2👍

Are you trying to make a template backend that will keep your templates in the data base and not in the file system? If so: https://docs.djangoproject.com/en/1.10/topics/templates/#custom-backends

I can see that in that StaticWebPage record you have a Django template, to render that you’ll need to treat is as a string and render that as you would when rendering strings.

from django.template import Template, Context

def index(request):
    index_object = StaticWebPage.objects.get(page_title='index')
    template = Template(index_object.page_content)
    return HttpResponse(
        content=template.render(Context({})),  # use the context or not
        content_type=None,
        status=200,
        reason=None,
        charset=None,
    )

(In Django 1.10) django.http.response.HttpResponse does not take request as the first argument (I added the args it takes for reference).

👤McAbra

Leave a comment