我感觉自己正在忽略一些小东西,但是我试图使用模板标记显示上传的服装文件的媒体文件:
INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','django.contrib.sites','allauth','allauth.account','allauth.socialaccount','apparelapp','import_export',]
settings.py
MEDIA_DIR = os.path.join(BASE_DIR,'media/')
MEDIA_URL = '/media/'
MEDIA_ROOT = "MEDIA_DIR"
models.py
class Uniform(models.Model):
category = models.CharField(choices = CATEGORY_CHOICES,max_length=11)
description = models.CharField(max_length = 50)
price = models.FloatField(max_length = 6)
size = models.CharField(choices=CLOTHES_SIZE,max_length=4,blank=True)
style = models.CharField(choices=STYLE_CHOICES,max_length=15,blank=True)
image = models.ImageField(upload_to='uniforms/')
html
<div class="row wow fadeIn">
{% for item in uniform %}
<div class="col-lg-3 col-md-6 mb-4">
<div class="card">
<div class="view overlay">
<img src="{{ item.image.url }}" alt="Oh no!">
<a>
<div class="mask rgba-white-slight"></div>
</a>
</div>
<div class="card-body text-center">
<label>
<h5>{{ item.description }}</h5>
</label>
<h5>
{% if item.description %}
<strong>
<label for="">{{ item.category }}</label>
</strong>
</h5>
{% endif %}
</div>
</div>
</div>
{% endfor %}
这条路径对我来说都是正确的,即使是settings.py
模型的image
设置并上传的Uniform
对象路径。我想念什么吗?
我查看了其他一些帖子,并在urls.py
中看到了一个建议,其中包括:
from django.conf.urls.static import static
urlpatterns = [
...
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
仍然没有运气