即使我没有收到任何错误,我的上传图像也无法显示: instead it looks like this
这是我的设置和代码:
settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static_cdn")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),"media")
型号:
class Post (models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
cover = models.ImageField(null=True, blank=True, upload_to='media/')
网址:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
观看次数:
def posts_create (request):
form = PostForm(request.POST or None, request.FILES or None)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
messages.success(request, "Successfully created")
return HttpResponseRedirect(instance.get_absolute_url())
context = {'form': form}
return render(request, 'post_form.html', context)
模板:
{% extends "base.html" %}
{% block title %} {{object.title}} | {{block.super}}{% endblock title%}
{% block content %}
<div class="center" style=" display: table; margin-right: auto; margin-left: auto;">
{% if object.cover %}
<img src="{{object.cover.url}}" class="img-responsive"/>
{% endif %}
<h1>{{title}} <small style="font-size: small"> {{object.timestamp}}</small> </h1>
{{object.content | linebreaks}}<br>
<div/>
{% endblock content%}
答案 0 :(得分:0)
我将静态和媒体文件的URL放在帖子/ URL中,而不是主url文件中,一旦我将它们重新放置,问题就消失了。