【问题标题】:how to render image and remove the html tags in content from django ck editor in django template如何从 django 模板中的 django ck 编辑器渲染图像并删除内容中的 html 标签
【发布时间】:2021-03-31 08:57:30
【问题描述】:

我尝试了很多次,但我没有得到任何解决方案来渲染没有 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>

   

【问题讨论】:

    标签: django django-models django-views django-templates django-ckeditor


    【解决方案1】:

    没有直接的方法可以访问 ck-editor 字段中的图像...但是如果您有指向 img 的方向,您可以像 /media/uploads/2020/12/21/my_img.jpg 一样获得它

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2011-12-01
      • 2018-02-11
      • 2011-10-17
      • 1970-01-01
      • 2011-09-30
      • 2010-12-15
      相关资源
      最近更新 更多