如何在 django 模板中的 django ck 编辑器中渲染图像并删除内容中的 html 标签

问题描述

我尝试了很多次,但我没有得到任何没有 html 标签的渲染图像和内容解决方案,我使用 {{content|safe}} 在模板上渲染内容,我尝试了 strip_tags 来移除 html 标签,但它对 url 不起作用,谁能告诉我为什么我在模板中渲染时得到 html 标签

Models.py
class Product(models.Model):
    name = models.RichTextUploadingField(max_length="125")
    description = models.RichTextUploadingField()

views.py
def poll():
  context = questions.object.all()
  return render(request,'quiz.html',context)

template:

    <html>
      <body>  
    
         {% for q in context %}
                <div id="section{{forloop.counter}}">
              <script type="text/javascript">
                    marks.push(Number("{{marks}}"));
                    neg_marks.push(Number("{{neg_marks}}"));
                  </script>
                  <p id="{{forloop.counter}}"><span>{{forloop.counter}}.{{q.question|linebreaks}}            
                 </span></p>
                  {% if q.figure %}
                 <img src="{{q.figure.url}}" alt="image" class="img-fluid" width="200px" height="200px"><br><br>
                   {% endif %}
          <input type="radio" id="{{forloop.counter}}option1" name="{{forloop.counter}}" value="1">
          <label for="{{forloop.counter}}option1">{{q.option_1|safe }}</label><br>
          <input type="radio" id="{{forloop.counter}}option2" name="{{forloop.counter}}" value="2">
          <label for="{{forloop.counter}}option2">{{q.option_2|safe}}</label><br>
           {% if q.option_3 != "" %}
           <input type="radio" id="{{forloop.counter}}option3" name="{{forloop.counter}}" value="3">
                  <label for="{{forloop.counter}}option3">{{q.option_3|safe}}</label><br>
                  {% endif %}
                  {% if q.option_4 != "" %}
           <input type="radio" id="{{forloop.counter}}option4" name="{{forloop.counter}}" value="4">
                  <label for="{{forloop.counter}}option4">{{q.option_4|safe}}</label><br>
                  {% endif %}
                  {% if q.option_5 != "" %}
          <input type="radio" id="{{forloop.counter}}option5" name="{{forloop.counter}}" value="5">
              <label for="{{forloop.counter}}option5">{{q.option_5|safe }}</label><br>
                  {% endif %}
                  {% if forloop.first %}
          <p role="button" class="btn btn-warning" id="next{{forloop.counter}}" onclick="next_section(this.id)"
                    style="float: right;">Next</p>
                  {% elif forloop.last %}
                  <p role="button" class="btn btn-warning" id="prev{{forloop.counter}}" onclick="prev_section(this.id)"
                    style="float:left;">PrevIoUs</p>
                  {% else %}
                  <p role="button" class="btn btn-warning" id="next{{forloop.counter}}" onclick="next_section(this.id)"
                    style="float: right;">Next</p>
        
                  <p role="button" class="btn btn-warning" id="prev{{forloop.counter}}" onclick="prev_section(this.id)"
                    style="float:left;">PrevIoUs</p>
                  {% endif %}
                  <br>
                  <hr>
    
            
      </body>
    </html>

   

解决方法

没有直接的方法来访问 ck-editor 字段中的图像......但是如果你有 img 的方向,你可以像 /media/uploads/2020/12/21/my_img.jpg

  • 我建议使用图像字段来存储您想要的图像的外部字段

  • 你有问题:context = questions.object.all() 应该是

context = {'context':questions.object.all()}