django - coercing to Unicode: need string or buffer, NoneType found
<p>I am using python 2.7 &amp; django 1.7.</p>

<p>I have a model that I want to return a field to be used in a template, however that field may be blank, which means that I receive this error when I attempt to place a condition statement to the return field.</p>

<p>Here is my code:</p>

<pre><code>class ContactDetails(FillableModelWithLanguageVersion):
    user = models.ForeignKey(User)
    contact_details_phone = models.CharField(null=True, blank=True, max_length=30)
    contact_details_mobile_phone = models.CharField(null=True, blank=True, max_length=30)
    contact_details_email_address = models.EmailField(null=True, blank=True, max_length=250)
    contact_details_linkedin_address = models.URLField(null=True, blank=True, max_length=250)
    contact_details_facebook_address = models.URLField(null=True, blank=True, max_length=250)
    contact_details_twitter_address = models.URLField(null=True, blank=True, max_length=250)
    contact_details_timestamp_added = models.DateTimeField(auto_now_add=True, auto_now=False)
    contact_details_timestamp_updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    def __unicode__(self):
        return
        if self.contact_details_mobile_phone:
            truncatechars(self.contact_details_mobile_phone, 30)
        elif self.contact_details_phone:
            truncatechars(self.contact_details_phone, 30)
        ......
</code></pre>

<p>When I apply the if elif condition above, I receive the following error:</p>

<p><strong>coercing to Unicode: need string or buffer, NoneType found</strong></p>

<p>I have read this <a href="https://stackoverflow.com/questions/16169035/coercing-to-unicode-need-string-or-buffer-nonetype-found-when-rendering-in-dja">post</a> and applied the suggested fixes, but this does not work for me. Here is an example of one attempt I have tried</p>

<pre><code>def __unicode__(self):
    return
    if self.contact_details_mobile_phone:
        unicode(truncatechars(self.contact_details_mobile_phone, 30)) or u''
    elif self.contact_details_phone:
        unicode(truncatechars(self.contact_details_phone, 30)) or u''
    .....
</code></pre>

<p>I continue to get the same error message. I have checked the data and the data is good. I am wondering if I have misused the if elif condition syntax?</p>

<p><strong>Does anyone have a suggestion I could attempt?</strong></p>