1

I want to save HTML code inside my Postgresql database so that I can directly send this code as HttpResponse instead of serving static HTML file. I did saved it inside database but following error keeps on coming. Error after returning html code

What is correct way of doing this task?

Edit1: code of views.py file

def index(request):

    index_object = StaticWebPage.objects.get(page_title='index')

    return HttpResponse(
        request,
        index_object.page_content
    )
3
  • Please (if you can) paste the code you are using as your view. Commented Mar 5, 2017 at 21:58
  • @McAbra Ok wait posting code Commented Mar 5, 2017 at 21:59
  • @McAbra added views.py code Commented Mar 5, 2017 at 22:04

1 Answer 1

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).

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

4 Comments

@aVIRA Please elaborate, only then I'll be able to aid you
after making GET request to url, output is None.
So my code is working (it's not broken like throwing an exception) but it did not resolve you problem? Does your StaticWebPage.objects.get(page_title='index').page_content equal to None by any chance?
no it is not equal to None. Now code i saved is plane HTML code. Can you please share your HTML code as gist so that I could test it.

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.