2015-10-16 80 views
1

我已經安裝了應用程序TinyMCE,並且所有程序都已啓動並正在運行。但有一個問題。發佈帶有圖像的HTML輸入時以純文本顯示。沒有HTML輸出到Django的博客文章

<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:

{% 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:

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 

我想有件事情我甲肝被遺忘了。那麼這裏有什麼問題?

回答

2

這是因爲html正在被轉義。例如,如果您讓匿名用戶在您的網站上發佈文章(或註釋),並讓他們編寫自己的html(並因此編寫javascript),則可能會發生不好的事情。

如果它只是你寫文章,隨意使用:

{{ post.bodytext|safe }} 

更多信息這個位置:

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

+0

你,先生,值得一啤酒!非常感謝。 – Qao

+0

沒問題的隊友!我們都有這個問題 – user1797792