我正在使用 Django 1.5.4
我是Django的新手,我试图显示通过管理面板上传的图片,但不幸的是,图片源代码中的网址字段为空,如果我更改{{1} } {{ article.image.url }}
,图片网址显示为
{{ article.image }}
当我点击图片链接时,它会显示
<img src="media/abyss.jpg" alt="" height="450"/>
请帮助我。
Settings.py文件
TypeError at /media/abyss.jpg
can only concatenate tuple (not "unicode") to tuple
Models.py文件
MEDIA_ROOT = (os.path.join(os.path.dirname(__file__), '..', 'media').replace('\\','/'),)
MEDIA_URL = '/media/'
Urls.py
class Article(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=255)
description = models.TextField()
content = models.TextField()
published = models.BooleanField(default=True)
image = models.ImageField(upload_to='media', blank=True)
created = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return u'%s' % self.title
class Meta:
ordering = ['-created']
def get_absolute_url(self):
return reverse('blog.views.article', args=[self.slug])
index.html
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.index'),
url(r'^blog/(?P<slug>[\w\-]+)/$', 'blog.views.article'),
url(r'^(.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
答案 0 :(得分:6)
MEDIA_ROOT
应该是字符串而不是元组:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '..', 'media').replace('\\','/')
尾随逗号使其成为元组:
>>> x = (1,)
>>> type(x)
<type 'tuple'>
>>> x + u'foo'
Traceback (most recent call last):
x + u'foo'
TypeError: can only concatenate tuple (not "unicode") to tuple