top of page
Search

Django: Use templates to dynamically render HTML

Writer's picture: Kunal PatilKunal Patil

Updated: Mar 16, 2021

Django is a Model-View-Template framework that uses Python for web development. Big names such as Google, Youtube, and Instagram use this framework. Django boasts its batteries-included feature, which is a bunch of features such as authentication and messaging that Django has. It follows the Convention Over Configuration pattern and the DRY pattern as well. Django is a great choice for security applications because it provides techniques and tools for developers to build a secure website or implements the security features in the framework itself, like preventing code execution in the template layer. Here is a more detailed post on Django: What is Django? Advantages and Disadvantages of using Django


In Django projects, we can run HTML code as a string from view.py.

However to render a whole page, we need more than just a string of HTML - so we use Templates.

Templates are essential to Django apps because they allow the implementer to:

- 'Plug in' Python code inside an HTML template

- Modulate and write cleaner code by separating the HTML and Python files


Example:

All table definitions for our schema go in 'models.py', but for this example's simplicity I have the class definition for Challenge in 'views.py'.

Passing in challenges in 'views.py' to our 'home.html' template:

While rendering the template in 'home.html', I use a foreach loop to access each challenge's own properties:

Rendered output using Bootstrap carousels at '{host}/home':


33 views0 comments

Recent Posts

See All

Commentaires


Post: Blog2_Post

©2021 by Today I Learned. Proudly created with Wix.com

bottom of page