Home > Net >  how to insert html tags into base template? (Django Python )
how to insert html tags into base template? (Django Python )

Time:12-20

I want to dynamically generate a page using the base template

I need to insert a piece of HTML code on the page

I have checkboxes and after selecting some, generate new ones according to favorites.

It doesn't work as it should :

def index(request):
    context = {'content': '<h1>hello</h1>'}
    return render(request, 'base.html', context)

on the HTML page :

<h1>hello</h1>

how to insert html tags into base template?

CodePudding user response:

you can use render:

views.py

from django.shortcuts import render

def index(request):
context = {'foo': 'bar'}
    return render(request,'yourapp_name/your_html.html',context)

your_html.html

{% extends 'yourapp_name/base.html' %} 

{% block content %}

Hello

{% endblock %}

remember your html files should have this path: yourapp_name/templates/yourapp_name/your_html.html

CodePudding user response:

the tags you want to use are django specific tags that neet to be converted by the django renderer. Put the html in a template file and render it as response:

def index(request):

   ....

 return render(request, "path/your_template.html", context)

please follow the django docs to understand where to place the templates so that django will find them.

  • Related