Django admin error : coercing to Unicode: need string or buffer, NoneType found

855 views
Skip to first unread message

slim

unread,
Apr 24, 2013, 5:37:56 AM4/24/13
to django...@googlegroups.com
Hello everyone,

I have this error since a long time but can't figure it out :

Caught TypeError while rendering: coercing to Unicode: need string or buffer, NoneType found

It happens in admin when I try to add or modify on one of my models (display works fine)


This is the model:


    class PS(models.Model):
        id_ps = models.IntegerField(null=True)
        client = models.ForeignKey(Client, null=True, blank=True)
        nom_du_site = models.CharField(max_length=250)
        rue_livraison = models.TextField(null=True)
        complement_adresse = models.TextField(null=True)
        code_postal_livraison = models.CharField(max_length=50, null=True)
        ville_livraison = models.CharField(max_length=200, null=True)
        pays_livraison = models.CharField(max_length=200, null=True)
        distributeur = models.CharField(max_length=50, null=True)
        tarif = models.CharField(max_length=250, null=True)
        type_tarif = models.CharField(max_length=50, null=True)
        type_installation = models.CharField(max_length=50, null=True)
    
        def __unicode__(self):
            return self.nom_du_site
        
        @property
        def pce(self):        
            try:
                return PCE.objects.filter(ps=self)[0]
            except IndexError:
                return
        #    
        def get_pce(self):
            from pce.models import PCE
            return PCE.objects.filter(ps=self).exclude(frequence='6M')
        #
        def get_pce_6m(self):
            from pce.models import PCE
            return PCE.objects.filter(ps=self,frequence='6M') 



Any idea what am i doing wrong here?


Traceback:

        Environment:
        
        Request Method: GET
        Django Version: 1.2.5
        Python Version: 2.7.2
        Installed Applications:
        ['django.contrib.auth',
         'django.contrib.contenttypes',
         'django.contrib.sessions',
         'django.contrib.sites',
         'django.contrib.messages',
         'django.contrib.admin',
         'easy_thumbnails',
         'django_extensions',
         'contentadmin',
         'south',
         'sentry',
         'sentry.client',
         'indexer',
         'file_uploader',
         'paging',
         'pagination',
         'consommation',
         'ps',
         'pce',
         'profil',
         'analytics',
         'document']
        Installed Middleware:
        ('annoying.middlewares.StaticServe',
         'django.middleware.common.CommonMiddleware',
         'django.contrib.sessions.middleware.SessionMiddleware',
         'django.middleware.csrf.CsrfViewMiddleware',
         'django.contrib.auth.middleware.AuthenticationMiddleware',
         'django.contrib.messages.middleware.MessageMiddleware',
         'pagination.middleware.PaginationMiddleware')
        
        
        Template error:
        In template /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
           Caught TypeError while rendering: coercing to Unicode: need string or buffer, NoneType found
           9 :             {% for field in line %}
        
        
           10 :                 <div{% if not line.fields|length_is:'1' %} class="field-box{% if not field.is_readonly and field.errors %} errors{% endif %}"{% 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.field.help_text %}
        
        
           23 :                         <p class="help">{{ field.field.field.help_text|safe }}</p>
        
        
           24 :                     {% endif %}
        
        
           25 :                 </div>
        
        
           26 :             {% endfor %}
        
        
           27 :         </div>
        
        
           28 :     {% endfor %}
        
        
           29 : </fieldset>
        
        
        Traceback:
        File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
          100.                     response = callback(request, *callback_args, **callback_kwargs)
        File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
          288.                 return self.admin_site.admin_view(view)(*args, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
          76.                     response = view_func(request, *args, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
          78.         response = view_func(request, *args, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
          190.             return view(request, *args, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
          21.             return decorator(bound_func)(*args, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
          76.                     response = view_func(request, *args, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
          17.                 return func(self, *args2, **kwargs2)
        File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in _commit_on_success
          299.                     res = func(*args, **kw)
        File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view
          909.         return self.render_change_form(request, context, form_url=form_url, add=True)
        File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in render_change_form
          692.         ], context, context_instance=context_instance)
        File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py" in render_to_response
          20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
        File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
          186.     return t.render(context_instance)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          173.             return self._render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in _render
          167.         return self.nodelist.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          796.                 bits.append(self.render_node(node, context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
          72.             result = node.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
          125.         return compiled_parent._render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in _render
          167.         return self.nodelist.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          796.                 bits.append(self.render_node(node, context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
          72.             result = node.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
          125.         return compiled_parent._render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in _render
          167.         return self.nodelist.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          796.                 bits.append(self.render_node(node, context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
          72.             result = node.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
          62.             result = block.nodelist.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          796.                 bits.append(self.render_node(node, context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
          72.             result = node.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
          174.                 nodelist.append(node.render(context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
          139.             return self.template.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          173.             return self._render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in _render
          167.         return self.nodelist.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          796.                 bits.append(self.render_node(node, context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
          72.             result = node.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
          174.                 nodelist.append(node.render(context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
          174.                 nodelist.append(node.render(context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
          260.             return self.nodelist_false.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          796.                 bits.append(self.render_node(node, context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
          72.             result = node.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
          260.             return self.nodelist_false.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/__init__.py" in render
          796.                 bits.append(self.render_node(node, context))
        File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
          72.             result = node.render(context)
        File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render
          91.             output = force_unicode(output)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_unicode
          66.                 s = unicode(s)
        File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in __unicode__
          408.         return self.as_widget()
        File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in as_widget
          450.         return widget.render(name, data, attrs=attrs)
        File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py" in render
          247.         output = [self.widget.render(name, value, *args, **kwargs)]
        File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in render
          447.         options = self.render_options(choices, [value])
        File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in render_options
          464.         for option_value, option_label in chain(self.choices, choices):
        File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __iter__
          898.                 yield self.choice(obj)
        File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in choice
          904.         return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
        File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in label_from_instance
          955.         return smart_unicode(obj)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in smart_unicode
          39.     return force_unicode(s, encoding, strings_only, errors)
        File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_unicode
          66.                 s = unicode(s)
        
        Exception Type: TemplateSyntaxError at /admin/ps/ps/add/
        Exception Value: Caught TypeError while rendering: coercing to Unicode: need string or buffer, NoneType found


Shawn Milochik

unread,
Apr 24, 2013, 10:10:12 AM4/24/13
to django...@googlegroups.com
Try iterating through your output in the view and look at field, field.content, and if field.help_text. Somewhere you have a null value which is None in Python. 

Since the traceback you posted (if it's complete) is all from Django's code and not yours, then the error must be in your data.

Wissal Khadrouf WBC

unread,
Apr 24, 2013, 10:39:13 AM4/24/13
to django...@googlegroups.com
That's what I was thinking,it's from the data 
But I dont know wich field and how to find out 


2013/4/24 Shawn Milochik <sh...@milochik.com>
Try iterating through your output in the view and look at field, field.content, and if field.help_text. Somewhere you have a null value which is None in Python. 

Since the traceback you posted (if it's complete) is all from Django's code and not yours, then the error must be in your data.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/r4VivI6pwOs/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Cordialement.


Wissal KHADROUF
Web Development Engineer
81 Avenue Mers sultan, Grand Casablanca, Maroc
0660 347 547


Shawn Milochik

unread,
Apr 24, 2013, 11:12:41 AM4/24/13
to django...@googlegroups.com
See what I said above about iterating through the fields in your view. Use logging, print statements, or dump output to a file.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
voicemail or fax: 206-350-8781

Wissal Khadrouf WBC

unread,
Apr 24, 2013, 11:17:01 AM4/24/13
to django-users
it's the admin view you're telling me to modify
I'm sorry I'm faily new to django and I'm afraid I will do something wrong!


2013/4/24 Shawn Milochik <sh...@milochik.com>

Shawn Milochik

unread,
Apr 24, 2013, 12:00:14 PM4/24/13
to django...@googlegroups.com

Ah, I missed that point. You could temporarily create an __init__ override in your model and put the code there.

Wissal Khadrouf WBC

unread,
Apr 24, 2013, 12:34:34 PM4/24/13
to django...@googlegroups.com
I went the hard way and changed all the null values untill I found out which one caused a problem
Thanks


2013/4/24 Shawn Milochik <sh...@milochik.com>

Ah, I missed that point. You could temporarily create an __init__ override in your model and put the code there.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/r4VivI6pwOs/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages