gpt4 book ai didi

django - 上传的图片不显示,路径错误

转载 作者:行者123 更新时间:2023-12-02 22:09:38 26 4
gpt4 key购买 nike

我在显示从管理面板上传的图像时遇到了问题。 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

这是 url 配置:

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)

设置.py:

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

View :

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。所以我想所有路由/模板都可以,模型有问题。

最佳答案

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}),
)

尝试使用上面给出的代码。

关于django - 上传的图片不显示,路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15535938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com