如何渲染 html 代码,该代码从模板中的模型渲染为 html
how to render html code which is render from model in template as html
我正在创建一个用于学习 django 的博客项目,我想知道是否可以在 model.textfield 中编写 html 代码,当在我的模板中获取它时,它呈现为 html 将整个代码编码为段落
让我用例子来解释你我有模型
class Movie(models.Model):
title = models.CharField(max_length=100, blank=False)
urltitle = models.SlugField(max_length=100, blank=False, unique=True)
info = models.TextField(blank=False,default=None)
title_image = models.ImageField(upload_to='movies',blank=False, null=True)
image_one = models.ImageField(upload_to='movies',blank=True, null=True)
image_two = models.ImageField(upload_to='movies',blank=True, null=True)
para_one = models.TextField(blank=True)
written_by = models.CharField(max_length=50, blank=True)
joined_date = models.DateTimeField(default=timezone.now,editable=False)
created = models.DateTimeField(auto_now=True)
如您在此模型中所见,如果我在其中编写代码,我有一个文本字段名称第一个
喜欢 Hello World 并保存它,然后在渲染它时保存我的模板它用 h1 标题渲染 hello world 而不是像这个 Hello World 作为段落
我希望你明白了,或者有没有办法做到这一点或与任何其他领域
如果您想将文本字段呈现为 html 代码,您可以像这样在 html 文件中使用安全过滤器
{{movie.para_one|safe}}
我正在创建一个用于学习 django 的博客项目,我想知道是否可以在 model.textfield 中编写 html 代码,当在我的模板中获取它时,它呈现为 html 将整个代码编码为段落
让我用例子来解释你我有模型
class Movie(models.Model):
title = models.CharField(max_length=100, blank=False)
urltitle = models.SlugField(max_length=100, blank=False, unique=True)
info = models.TextField(blank=False,default=None)
title_image = models.ImageField(upload_to='movies',blank=False, null=True)
image_one = models.ImageField(upload_to='movies',blank=True, null=True)
image_two = models.ImageField(upload_to='movies',blank=True, null=True)
para_one = models.TextField(blank=True)
written_by = models.CharField(max_length=50, blank=True)
joined_date = models.DateTimeField(default=timezone.now,editable=False)
created = models.DateTimeField(auto_now=True)
如您在此模型中所见,如果我在其中编写代码,我有一个文本字段名称第一个 喜欢 Hello World 并保存它,然后在渲染它时保存我的模板它用 h1 标题渲染 hello world 而不是像这个 Hello World 作为段落
我希望你明白了,或者有没有办法做到这一点或与任何其他领域
如果您想将文本字段呈现为 html 代码,您可以像这样在 html 文件中使用安全过滤器
{{movie.para_one|safe}}