简体   繁体   English

没有HTML输出到Django的博客文章

[英]No HTML output to blog post with Django

I've installed the app TinyMCE and all is up and running. 我已经安装了应用程序TinyMCE,一切都已启动并正在运行。 There's one problem though. 不过有一个问题。 When posting the HTML input with an image is displayed in plain text. 发布时,带有图像的HTML输入以纯文本显示。

 <p><img src="https://www.djangoproject.com/s/img/logos/django-logo-negative.png" alt="django" /></p> <p>Lorem ipsum dolor sit amet, inani clita efficiantur sed et, ad honestatis complectitur vim.</p> 

index.html: index.html的:

{% for post in posts %}
          <div class="post">
          <h2>{{ post.title }}</h2>
          <p>Posted on {{ post.timestamp }} by {{ post.author }}</p>
          <p>{{ post.bodytext }}</p>
          </div>
      {% endfor %}

models.py: models.py:

from django.db import models
from django.utils import timezone
from django.conf import settings
from tinymce.models import HTMLField

class posts(models.Model):
    author = models.CharField(max_length = 30)
    title = models.CharField(max_length = 100)
    bodytext = HTMLField()
    timestamp = models.DateTimeField(default = timezone.now) # sets a default date & time value when creating a new post

    class Meta:
        verbose_name_plural = "posts" # sets the plural name of posts (default = postss)
    def __str__(self):
        return self.title

I guess there's something I have forgotten to do. 我想有些事情我忘了做。 So what is wrong here? 那么,这里出了什么问题?

It's because the html is being escaped. 这是因为html被转义了。 For example if you let anonymous users post articles (or comments for example) on your site, and let them write their own html (and thus javascript), bad things can happen. 例如,如果让匿名用户在您的网站上发布文章(或评论),并让他们编写自己的html(因此是javascript),则可能会发生不良情况。

If it's just you writing the Posts, feel free to use: 如果只是您撰写帖子,请随时使用:

{{ post.bodytext|safe }}

More info about this here: 有关此的更多信息:

https://docs.djangoproject.com/en/1.8/ref/templates/language/#automatic-html-escaping https://docs.djangoproject.com/en/1.8/ref/templates/language/#automatic-html-escaping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM