django template render dict

# views.py
from django.shortcuts import render

def my_view(request):
    my_dict = {'key1': 'value1', 'key2': 'value2'}
    return render(request, 'my_template.html', {'my_dict': my_dict})
<!-- my_template.html -->
<!DOCTYPE html>
<html>
<head>
    <title>My Template</title>
</head>
<body>
    <h1>Values from the Dictionary:</h1>
    <ul>
        <li>{{ my_dict.key1 }}</li>
        <li>{{ my_dict.key2 }}</li>
    </ul>
</body>
</html>
  1. In the views.py file, import the render function from django.shortcuts.

  2. Define a view function (my_view) that takes a request parameter.

  3. Create a dictionary (my_dict) with key-value pairs.

  4. Use the render function to render the my_template.html template. Pass the request object, the template name, and a dictionary containing the data to be rendered in the template.

  5. In the my_template.html file, define an HTML document.

  6. Use the {{ my_dict.key1 }} template tag to access and display the value associated with the key 'key1' in the my_dict dictionary.

  7. Similarly, use the {{ my_dict.key2 }} template tag to access and display the value associated with the key 'key2' in the my_dict dictionary.

  8. The rendered HTML will display a heading and an unordered list with the values from the dictionary.