我似乎无法显示数据库中的图像。在文件之后,“视图”和“模型”具有正常工作的功能,因为我可以显示名称和类型,但似乎找不到图像。
我在index.html中的当前行是这样的:
{% for skill in webdev %}
{{skill.name}}
<img src="{{skill.image}}">
{% endfor %}
views.py:
def index(request):
skill_list = Skill.objects.all()
context = { 'webdev': skill_list.filter(type=1),
'software': skill_list.filter(type=2),
'database': skill_list.filter(type=3)
}
return render(request, 'skills/index.html', context)
models.py:
from django.db import models
# Create your models here.
class Skill(models.Model):
name = models.CharField(max_length=100)
class Type(models.IntegerChoices):
WEBDEV = 1
SOFTWARE = 2
DATABASE = 3
type = models.IntegerField(choices=Type.choices)
image = models.ImageField(upload_to='img', blank=True)
def __str__(self):
return self.name
settings.py:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'skills/media')
urlpatterns具有+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)