Django - 来自MEDIA_URL的图片没有显示在模板上?
所以我已经阅读了关于这个问题的一些堆栈溢出的答案,但没有一个能够解决我的问题。我设置了MEDIA_ROOT,MEDIA_URL,STATIC_ROOT和STATIC_URL,但由于某些原因,我的media_url中的图像没有显示在我的模板上,而来自我的static_url的图像是。Django - 来自MEDIA_URL的图片没有显示在模板上?
我的代码如下以及进一步的解释。
Settings.py
STATICFILES_DIRS = (
('/static/',
'/home/joe/Documents/exchange/Texchange/textchange/static',),
('/media/',
'/home/joe/Documents/exchange/Texchange/textchange/media/',),
)
MEDIA_ROOT = '/home/joe/Documents/exchange/Texchange/textchange/media/'
MEDIA_URL = '/media/'
STATIC_ROOT = '/home/joe/Documents/exchange/Texchange/textchange/'
STATIC_URL = '/static/'
Models.py
class Posting(models.Model):
textbook = models.ForeignKey(Textbook)
condition = models.CharField(max_length = 200)
price = models.DecimalField(max_digits=5, decimal_places=2)
user = models.ForeignKey(User)
image = models.ImageField(upload_to='postingpics/%Y/%m/%d', default="../../static/textchange/nophoto.jpg")
post_date = models.DateTimeField('date_posted')
def __str__(self):
return str(self.textbook)
def was_posted_recently(self):
return self.post_date >= timezone.now() - datetime.timedelta(days=1)
was_posted_recently.admin_order_field = 'post_date'
was_posted_recently.boolean = True
was_posted_recently.short_description = 'Posted recently'
Template.html
<div class="row marketing">
<div class="col-lg-6">
<h3>Textbook Info</h3>
<p><strong>Textbook: </strong>{{ posting.textbook.textbook_name }}</p>
<p><strong>Condition: </strong>{{ posting.condition }}</p>
<p><strong>Price: </strong>{{ posting.price }}</p>
<p><strong>Class: </strong>{{ posting.textbook.class_name }}</p>
<p><strong>ISBN: </strong>{{ posting.textbook.isbn }}</p>
<p><strong>Author: </strong>{{ posting.textbook.author }}</p>
<h3>Contact Info</h3>
<p><strong>User: </strong>{{ posting.user.username }}</p>
<p>Button to contact</p>
</div>
<div class="col-lg-6">
{% block content %}
<img src="{{ posting.image.url }}">
<p>{{ posting.image.url }}</p>
{% endblock %}
</div>
</div>
所以你可以看到,如果我没有上传图片的帖子它会转到默认图像,该图像存储在静态/文本更改中。该图像将显示在模板上。如果我上传图片,图片将不会显示在模板上。在我的template.html中,我在段落标签中显示了{{posting.image.url}}文字,以查看不同图片的网址。
当使用static/textchange中的默认图像时,url为:/../static/textchange/nophoto.jpg,图像显示正常。
当我尝试在模板中显示上传的图像时,网址为:/media/postingpics/2015/09/20/Star-Wars-Sith-Code-Wallpaper-2_dlPszgQ.jpg并且没有图像出现。 ..
我觉得很奇怪,静态图像在静态前面有/../,但我不知道为什么只显示静态文件夹中的图像。
任何帮助,将不胜感激。
MEDIA_URL不是由Django自动提供的,你必须手动设置它。 https://docs.djangoproject.com/en/1.8/howto/static-files/#serving-files-uploaded-by-a-user-during-development
而且,你对你的静态和媒体文件设置为奇:
- 你MEDIA_ROOT是你STATIC_ROOT内。为什么?
- 你的STATICFILES_DIRS在你的STATIC_ROOT里面。这没有任何意义。 STATICFILES_DIRS应该被收集到你的STATIC_ROOT中。
- 您的MEDIA_ROOT在STATICFILES_DIRS中设置为静态文件目录。媒体文件根据定义不是静态文件。删除这个。
它看起来像你想要更多的东西是这样的:
STATICFILES_DIRS = (
'/home/joe/Documents/exchange/Texchange/textchange/static',
)
MEDIA_ROOT = '/home/joe/Documents/exchange/Texchange/textchange/media/'
MEDIA_URL = '/media/'
STATIC_ROOT = '/home/joe/Documents/exchange/Texchange/textchange/static_root/'
STATIC_URL = '/static/'
假设Texchange/textchange
是你的项目的根。
嗯,我有一个媒体文件夹和我的应用程序文件夹内的静态文件夹。但如果我把静态结束STATIC_ROOT我的CSS文件停止加载... – Programmingjoe
我服务的媒体文件就像你说的,它的工作原理:) – Programmingjoe
STATIC_ROOT是不是真正用于开发,该文件夹只是存在(如果我记得正确)。只要'static'文件夹在STATICFILES_DIRS中就像上面一样,它将被提供 – mindcruzer