我在显示从管理面板上传的图片时出现问题。 Django呈现错误的路径,可能是由于我的配置错误...
这是我的模型定义:
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:
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
属性返回没有这个重要目录的路径?
编辑:我的模型出了问题。它无法识别 upload_to 目录:
$ ./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
。所以我猜所有路由/模板都没问题,模型出了问题。
答案 0 :(得分:2)
在 views.py :
中from django.shortcuts import render_to_response, RequestContext
def slider(request):
articles = Article.objects.order_by('-created_at')[:5]
return render_to_response('articles/slider.html', locals(), context_instance = RequestContext(request)
在 slider.html :
中{% for article in articles %}
<img src="{{ MEDIA_URL }}{{ article.image.url }}" alt="" />
{% endfor %}
在 urls.py :
from django.conf import settings
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
尝试使用上面给出的代码。
答案 1 :(得分:2)
以上解决方案完全可以。我的问题是用JSON编写的不正确配置的灯具并加载到数据库中。我认为在你将articles
目录作为upload_to
kwarg传递时,它将被动态加载。 不是。它仅在保存图像时使用,在从数据库加载图像时被忽略。因此,如果我有
"image":"slide.jpg"
我把它改为:
"image":"articles/slide.jpg"
它有效。事实上,它一直都有效,但我在django官方文档中遗漏了一个注释。
答案 2 :(得分:1)
您是否尝试使用上传方法:
def upload_path(self, filename):
return 'media/articles/%s' % filename
class Article(models.Model):
...
image = models.ImageField(upload_to=upload_path, null=True, blank=True)
...