微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 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()}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?