我正在 Django 3.0 框架上创建博客。所以我遇到了从数据库获取图像的问题。我可以从管理员上传图像,并将其上传到我想要的目录中。当我输入要发布的图像 URL 时,我可以从浏览器检查中看到目录。甚至文件的名称,但图像没有显示出来。
设置.py
MEDIA_URL = '/uploadfiles/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/uploadfiles')
模型.py
STATUS = (
(0,"Drafted"),
(1, "Published")
)
class Article(models.Model):
article_title = models.CharField('სიახლის სახელი', max_length=200)
article_text = models.TextField('კონტენტი')
pub_date = models.DateTimeField('თარიღი')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
status = models.IntegerField(choices=STATUS, default=0)
updated_on = models.DateTimeField(auto_now=True)
slug = models.SlugField(max_length=200, unique=True)
imagecover = models.ImageField(upload_to='uploadfiles/postimages/', default='img-about1.jpg')
def __str__(self):
return self.article_title
def Recently_published(self):
return self.pub_date >= (timezone.now() - datetime.timedelta(7))
html文件
<div class="blog-post-image">
<img src="{{a.imagecover.url}}" alt="image" class="img-responsive center-block post_img" />
</div>
项目网址
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('article/', include('article.urls')),
path('admin/', admin.site.urls),
path('grappelli/', include('grappelli.urls')), # grappelli URLS
]