Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - get HTML output into a variable

instead of using render_to_response which will send the HTML output back to browser.

I would like to take the results, generate HTML (using templates) & output the html into a variable in my views.py. How can I do this?

like image 737
Srikar Appalaraju Avatar asked Jan 06 '11 03:01

Srikar Appalaraju


People also ask

How to pass data from HTML to view in Django?

You can create a HTML form and can submit the form using post method if you do not want to pass values in url. In the same way you can pass multiple parameters. add {% csrf_token %} to the form.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

How do I comment out code in Django?

To comment out a block of code in a Django template, use the {% comment %} and {% endcomment %} tags and pass the code to not render between the tags.

What is URL tag in Django?

The URL template tag is a typical type of Tag in the Django Template Language framework. This tag is specifically used to add View URLs in the Template Files.


1 Answers

SOLVED! the way to do this -

from django.template.loader import render_to_string rendered = render_to_string('my_template.html', { 'foo': 'bar' }) 

thanks to ned for pointing to the Django docs

like image 189
Srikar Appalaraju Avatar answered Sep 16 '22 20:09

Srikar Appalaraju