【发布时间】:2020-08-06 13:57:18
【问题描述】:
我确信我显然做了一些非常愚蠢的事情,但是当我尝试在我的博客网站上上传我的个人资料图片时,即使我通过管理页面添加了一张图片,图片也不会出现..
所以这就是发生的事情,
博客图片:
Kimmc6008 旁边的图片,我应该看到我的图片,但不知何故失败了..
这是代码,如果需要我会上传更多。 模型.py
class Author(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_picture = models.ImageField()
def __str__(self):
return self.user.username
class CategoryBlog(models.Model):
title = models.CharField(max_length=20)
def __str__(self):
return self.title
class Post(models.Model):
title = models.CharField(max_length=100)
overview = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
comment_count = models.IntegerField(default=0)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
thumbnail = models.ImageField()
categories = models.ManyToManyField(CategoryBlog)
featured = models.BooleanField(null=True)
def __str__(self):
return self.title
views.py
def blog(request):
post_list = Post.objects.all()
context = {
'post_list': post_list
}
return render(request, 'blog.html', context)
还有我的html
<!-- post -->
{% for post in post_list %}
<div class="post col-xl-6">
<div class="post-thumbnail"><a href="/post"><img src="{{post.thumbnail.url}}" alt="..." class="img-fluid"></a></div>
<div class="post-details">
<div class="post-meta d-flex justify-content-between">
<div class="date meta-last">20 May | 2016</div>
{% for cat in post.categories.all %}
<div class="category"><a href="#"></a>{{ cat }}</div>
{% endfor %}
</div><a href="/post">
<h3 class="h4">{{ post.title }}</h3></a>
<p class="text-muted">{{ post.overview }}</p>
<footer class="post-footer d-flex align-items-center">
<a href="#" class="author d-flex align-items-center flex-wrap">
<div class="avatar"><img src="{{ post.author.profile_picture.url }}" alt="..." class="img-fluid"></div>
<div class="title"><span>{{ post.author.user.username }}</span></div>
</a>
<div class="date"><i class="icon-clock"></i>{{ post.timestamp|timesince }} ago</div>
<div class="comments meta-last"><i class="icon-comment"></i>{{ post.comment_count }}</div>
</footer>
</div>
</div>
{% endfor %}
感谢您的帮助!!
编辑 我注意到我的 settings.py 中缺少 MEDIA_ROOT。 会是这个原因吗?
【问题讨论】:
-
试试
-
@noob 嗨!感谢您的评论。不幸的是,那并没有这样做另外,我注意到我缺少 MEDIA_ROOT,这就是原因吗?
-
是添加 settings.py MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')