ajk*_*hol 1 python django unicode
我正在使用Django 1.5.4
我是Django的新手,我试图显示通过管理面板上传的图像,但遗憾的是图像源代码中的url字段为空,如果我更改{{ article.image.url }}为{{ article.image }},则图像网址显示为
<img src="media/abyss.jpg" alt="" height="450"/> 
当我点击图片链接时,它说
TypeError at /media/abyss.jpg
can only concatenate tuple (not "unicode") to tuple
请帮我.
Settings.py文件
 MEDIA_ROOT = (os.path.join(os.path.dirname(__file__), '..', 'media').replace('\\','/'),)
 MEDIA_URL = '/media/'
Models.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])
Urls.py
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}),
                       )
的index.html
{% extends "blog/base.html" %}
{% block content %}
    <h1>PyStack</h1>
    <div>
        {% for article in articles %}
            <img src="{{ article.image.url }}" alt="" height="450"/>
            <h2><a href="{{ article.get_absolute_url }}">{{ article.title|capfirst }}</a></h2>
            <p>{{ article.description }}</p>
            <hr/>
        {% endfor %}
    </div>
{% endblock %}
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