0

In my django project I have a model with a field that contain html code as text:

html_test = models.TextField()

for example in this field could be:

<html>
    <header><title>This is title</title></header>
    <body>
        Hello world
    </body>
</html>

I want to retrive this code and render dynamically showing the corresponding html page without create a file or save it on disk. How can i render an html code in memory for display page and then destroy it?

2
  • Your question is unclear. If it's in your field already, why can't you just display it? Commented Oct 11, 2019 at 7:39
  • ..the problem is, how can i display the html page from my data? Commented Oct 11, 2019 at 7:50

2 Answers 2

1

I'm still not sure what your actual question is. You retrieve the model from the db and return the data to the user, just like anything else. For example, a view might look like this:

def my_view(request, pk):
    obj = MyModel.objects.get(pk=pk)
    return HttpResponse(obj.html_test)
Sign up to request clarification or add additional context in comments.

Comments

0

Using jinja2 as the templating engine,

class YourClassName(generic.TemplateView):
    template_name = 'your_template.jinja'

    def get_context_data(self, **kwargs):
        kwargs['html_data'] = MyModel.objects.get(pk=pk).html_test
        return super(YourClassName, self).get_context_data(**kwargs)

In your your_template.jinja

<html>
<header><title>This is title</title></header>
<body>
    {{html_data}}
</body>

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.