2015-06-19 36 views
3

我有一個正在運行的Django站點和正在運行的管理員,但當我嘗試使用管理站點從模型「Article」創建新對象時除外。當我嘗試創建一個新的「文章」我得到這個錯誤:Django Admin:TypeError:__str__返回的非字符串(類型FieldFile)

TypeError at /admin/blog/article/add/ 

__str__ returned non-string (type FieldFile) 
Exception Type:  TypeError 
Exception Value: __str__ returned non-string (type FieldFile) 
Exception Location:  /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/encoding.py in force_text, line 90 

下面是奇怪的事情:這是今天上午的工作。我嘗試安裝django-markdown,並開始出現此錯誤。我無法讓它工作,所以我卸載了django-markdown並將所有代碼恢復爲原來的(以前的功能),並進行了適當的遷移。

但我仍然收到錯誤。

這裏的追溯和渲染錯誤:

Error during template rendering 

In template /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19 
__str__ returned non-string (type FieldFile) 
9    {% for field in line %} 
10     <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}> 
11      {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %} 
12      {% if field.is_checkbox %} 
13       {{ field.field }}{{ field.label_tag }} 
14      {% else %} 
15       {{ field.label_tag }} 
16       {% if field.is_readonly %} 
17        <p>{{ field.contents }}</p> 
18       {% else %} 
19 

     {{ field.field }} 



20       {% endif %} 
21      {% endif %} 
22      {% if field.field.help_text %} 
23       <p class="help">{{ field.field.help_text|safe }}</p> 
24      {% endif %} 
25     </div> 
26    {% endfor %} 
27   </div> 
28  {% endfor %} 
29 </fieldset> 
Traceback Switch to copy-and-paste view 

    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/handlers/base.py in get_response 

            response = response.render() 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/response.py in render 

           self.content = self.rendered_content 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/response.py in rendered_content 

          content = template.render(context, self._request) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/backends/django.py in render 

          return self.template.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

             return self._render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in _render 

          return self.nodelist.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            bit = self.render_node(node, context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node 

           return node.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render 

          return compiled_parent._render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in _render 

          return self.nodelist.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            bit = self.render_node(node, context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node 

           return node.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render 

          return compiled_parent._render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in _render 

          return self.nodelist.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            bit = self.render_node(node, context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node 

           return node.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render 

            result = block.nodelist.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            bit = self.render_node(node, context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node 

           return node.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render 

            result = block.nodelist.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            bit = self.render_node(node, context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node 

           return node.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render 

               nodelist.append(node.render(context)) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render 

            return template.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            return self._render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in _render 

          return self.nodelist.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            bit = self.render_node(node, context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node 

           return node.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render 

               nodelist.append(node.render(context)) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render 

               nodelist.append(node.render(context)) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render 

            return nodelist.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            bit = self.render_node(node, context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node 

           return node.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render 

            return nodelist.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render 

            bit = self.render_node(node, context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node 

           return node.render(context) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render 

           output = force_text(output) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/encoding.py in force_text 

             s = six.text_type(s) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/html.py in <lambda> 

          klass.__str__ = lambda self: mark_safe(klass_str(self)) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/forms.py in __str__ 

          return self.as_widget() 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/forms.py in as_widget 

          return force_text(widget.render(name, self.value(), attrs=attrs)) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/contrib/admin/widgets.py in render 

           'widget': self.widget.render(name, value, *args, **kwargs), 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/widgets.py in render 

          options = self.render_options(choices, value) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/widgets.py in render_options 

          for option_value, option_label in chain(self.choices, choices): 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/models.py in __iter__ 

            yield self.choice(obj) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/models.py in choice 

          return (self.field.prepare_value(obj), self.field.label_from_instance(obj)) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/models.py in label_from_instance 

          return smart_text(obj) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/encoding.py in smart_text 

         return force_text(s, encoding, strings_only, errors) 

     ... 
    ▶ Local vars 
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/encoding.py in force_text 

             s = six.text_type(s) 

     ... 
    ▶ Local vars 

這裏是我的第二條型號:

class Article(models.Model): 
    author = models.ForeignKey(Author, blank=True, null=True) 
    title = models.CharField(max_length=200) 
    slug = models.SlugField(max_length=200, blank=True, null=True) 
    article_category = models.OneToOneField(ArticleCategory, blank=True, null=True) 
    body_en = models.TextField(blank=True, null=True) 
    body_fr = models.TextField(blank=True, null=True) 
    is_review = models.BooleanField(default=False) 
    publish = models.BooleanField(default=True) 
    date_created = models.DateTimeField(blank=True, null=True) 
    modified = models.DateTimeField(auto_now=True) 
    tags = models.ManyToManyField(Tag, blank=True, null=True) 
    work_link = models.ForeignKey(Work, null=True, blank=True) 
    artist_link = models.ForeignKey(Artist, null=True, blank=True) 
    images = models.ManyToManyField(ArticleImage) 

    def __str__(self): 
     return self.title 

    class Meta: 
     ordering = ["-date_created"] 

admin.py:

from django.contrib import admin 
from blog.models import Article, Tag, Author, ArticleCategory, ArticleImage 

# Register your models here. 
admin.site.register(Article) 
admin.site.register(Author) 
admin.site.register(Tag) 
admin.site.register(ArticleCategory) 
admin.site.register(ArticleImage) 

我幾乎覺得自己卸載,然後就重新 - 用當前的設置開始整個事情,但我更願意找出我搞砸了什麼,然後解決它。

任何幫助,非常感謝。

馬特

回答

9

不能看到局部變量就很難診斷問題。當需要在管理員中爲某個小部件提供選項標籤時,某個模型會返回FieldFile而不是字符串,請選擇我認爲的。

檢查您的模型上的str方法。有一個FileField將是罪魁禍首。

+0

但錯誤說「FieldFile」,而不是「FileField」。它們是一樣的嗎? –

+0

「當您訪問模型上的FileField時,將爲您提供一個FieldFile實例作爲訪問基礎文件的代理」 – miyamoto

+0

「修復此問題。它發生在我遇到由於Django降價安裝而引起的錯誤時,我將其歸因於此。我將字符串方法更改爲圖像標題,現在它工作正常。 –

相關問題