【问题标题】:django-summernote view uploaded image from templatedjango-summernote 从模板查看上传的图片
【发布时间】:2019-07-01 22:31:16
【问题描述】:

我正在尝试在从 summernote 上传时查看我上传的图片。

这是我的 settings.py 的静态和媒体:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_ROOT = "static/"
MEDIA_URL = "/./"

这个articles.html 模板很好地显示了我的静态文件:

{% extends 'base.html' %}
{% load static from staticfiles %}

{% block content %}
  {% for post in object_list %}
  <div class="card rounded border-1 border-secondary">
    <div class="card-header bg-dark text-light">
        <h4>{{ post.title }}</h4>
        <small>Posted: {{ post.date }}</small>
    </div>
    <div class="card-body bg-light">
      <img src={{ post.image.url }} width="300" height="200">
      <p class="card-text">{{ post.description }}</p>
    </div>
    <div class="card-body bg-light">
      <a href="{% url 'post_detail' post.pk %}" class="card-link">Read more...</a>
    </div>
  </div>
  <p></p>
  {% endfor %}
{% endblock content %}

我需要在我的 post_detail.html post.body 中嵌入图片(|仅用于测试安全):

{% extends 'base.html' %}

{% block content %}
  <div class="post-entry">
    <h2>{{ post.title  }}</h2>
    <p>{{ post.body|safe }}</p>
  </div>
{% endblock content %}

当我尝试用我得到的图像更新 post.body 时:

[07/Feb/2019 12:43:24] "GET /admin/blog/post/2/change/ HTTP/1.1" 200 10150
[07/Feb/2019 12:43:24] "GET /admin/jsi18n/ HTTP/1.1" 200 3185
[07/Feb/2019 12:43:25] "GET /summernote/editor/id_body/ HTTP/1.1" 200 6349
[07/Feb/2019 12:43:27] "GET /static/summernote/lang/summernote-en-US.min.js?_=1549561406677 HTTP/1.1" 200 27
[07/Feb/2019 12:43:38] "POST /summernote/upload_attachment/ HTTP/1.1" 200 179
Not Found: /django-summernote/2019-02-07/c4ad147f-01d0-464f-ab2d-e2ce60d5c62f.jpg
[07/Feb/2019 12:43:38] "GET /django-summernote/2019-02-07/c4ad147f-01d0-464f-ab2d-e2ce60d5c62f.jpg HTTP/1.1" 404 4071
[07/Feb/2019 12:43:42] "POST /admin/blog/post/2/change/ HTTP/1.1" 302 0
[07/Feb/2019 12:43:42] "GET /admin/blog/post/ HTTP/1.1" 200 4758
[07/Feb/2019 12:43:43] "GET /admin/jsi18n/ HTTP/1.1" 200 3185

当我检查 post_detail.html 页面时,没有图像,但从链接嵌入时可以正常工作。

我不想更改我的媒体根目录或网址,以避免破坏其在网站其他部分的使用。

尝试搜索了很长时间,但我无法找到解决此问题的方法。

【问题讨论】:

    标签: python django python-3.x django-templates summernote


    【解决方案1】:

    那么……

    有时只是尝试更多的东西比几个小时的搜索效果更好。

    将我的 MEDIA_URL 更改为:

    MEDIA_URL = "/django-summernote/"
    

    解决了我所有的问题,我可以将图像上传到数据库,并根据需要将它们用于我的帖子缩略图和帖子正文中。

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 2017-07-16
      • 1970-01-01
      • 2014-03-04
      • 2015-12-02
      • 1970-01-01
      • 2016-06-13
      • 2016-01-02
      • 2019-10-08
      相关资源
      最近更新 更多