上传的图片未显示,路径错误 [英] Uploaded images not showing, wrong path

查看:251
本文介绍了上传的图片未显示,路径错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显示从管理面板上传的图像时出现问题. Django呈现了错误的路径,可能是由于我在某处的配置错误...

I've got a problem with displaying images that were uploaded from admin panel. Django renders wrong path, probably due to my configuration mistake somewhere...

这是我的模型定义:

class Article(models.Model):
    """News article, displayed on homepage to attract users"""
    class Meta:
        db_table = 'article'
    title = models.CharField(max_length=64)
    headline = models.CharField(max_length=255)
    content = HTMLField()
    image = models.ImageField(upload_to = 'articles/', null=True, blank=True)
    active = models.BooleanField()
    created_at = models.DateTimeField()
    def __unicode__(self):
        return self.title

这是网址配置:

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
# some stuff
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py:

settings.py:

PROJECT_DIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(PROJECT_DIR, "media")
MEDIA_URL = '/media/'

视图:

def slider(request):
    context = Context ({ 'articles': Article.objects.order_by('-created_at')[:5] })
    return render(request, 'articles/slider.html', context)

和模板:

{% for article in articles %}
  <img src="{{ article.image.url }}" alt="" />

我希望django渲染http://127.0.0.1:8000/media/articles/slide-02.jpg,但是现在它渲染http://127.0.0.1:8000/media/slide-02.jpg.我已经在模型类中定义了upload_to=articles/.那么,为什么article.image.url属性返回的路径却没有这个重要目录?

I would expect django to render http://127.0.0.1:8000/media/articles/slide-02.jpg but now it renders http://127.0.0.1:8000/media/slide-02.jpg. I've defined the upload_to=articles/ in the model class. So why does the article.image.url attribute return a path without this significant directory?

我的模型出了点问题.无法识别 upload_to 目录:

edit: I've got something wrong with my model. It doesn't recognize the upload_to directory:

$ ./manage.py shell
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from articles.models import Article
>>> Article.objects.all()
[<Article: Blackened Recordings Launches>, <Article: The Rockin' Return of Deep Purple>, <Article: See Emily Day 20th April>, <Article: Celebration Day Premieres In Four Countries Announced>, <Article: Waging heavy innovation>, <Article: Rush to play festival D'Ete, Quebec City>]
>>> Article.objects.all().get(pk=1)
<Article: Blackened Recordings Launches>
>>> Article.objects.all().get(pk=1).image
<ImageFieldFile: slide-03.jpg>
>>> Article.objects.all().get(pk=1).image.path
u'/var/www/django/djninja/djninja/media/slide-03.jpg'
>>> Article.objects.all().get(pk=1).image.url
'/media/slide-03.jpg'

同样,它应该是media/articles/slide-03.jpg而不是media/slide-03.jpg.所以我想所有的路由/模板都可以,模型有问题.

Again, it should be media/articles/slide-03.jpg instead of media/slide-03.jpg. So I guess all routing/template is ok, something's wrong with the model.

推荐答案

上述解决方案完全可以.我的问题是使用JSON编写的未正确配置的Fixtures并已加载到数据库中.我认为在您将articles目录作为upload_to kwarg传递时,它将被动态加载. 不是.它仅在保存图像期间使用,在从数据库加载图像期间将被忽略.因此,如果我有

Above solution is perfectly ok. My problem was the improperly configured fixtures written in JSON and loaded into the database. I thought that in you pass articles directory as the upload_to kwarg, it'll be dynamically loaded. It's not. It is used only during saving the image and is ignored during loading the image from the database. Thus, if I had

"image":"slide.jpg"

我将其更改为:

"image":"articles/slide.jpg"

它奏效了.实际上,它一直都有效,但是我在Django官方文档中错过了关于它的说明.

And it worked. In fact, it worked all the time, but I miss a note on that in django official docs.

这篇关于上传的图片未显示,路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆