django-summernote视图从模板上传图像

时间:2019-02-07 18:03:41

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

从Summernote上传时,我正在尝试查看上传的图像。

这是我用于设置的静态文件和媒体。

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

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

此article.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页面时,没有图像,但是从链接中嵌入时效果很好。

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

试图搜索了很长一段时间,但我找不到解决方法。

1 个答案:

答案 0 :(得分:0)

那好吧...

有时候,尝试更多的事情比花费数小时的搜索效果更好。

将我的MEDIA_URL更改为:

MEDIA_URL = "/django-summernote/"

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