1

In a Django project, I am trying to pass the url to a want instance. Comments are applied to a Want. I have been trying to figure out this error but am stumped.

This function:

     def comment_email(request, comment, **kwargs):
         want = get_object_or_404(Want, id=comment.object_pk)
         url = want.get_absolute_url
         print url

Is throwing this error

Environment:

Request Method: POST
Request URL: http://localhost:8000/comments/post/
Django Version: 1.2.3
Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.admin',
 'django.contrib.comments',
 'django.contrib.markup',
 'src',
 'lib.tagging',
 'lib.markdown',
 'lib.avatar',
 'ajaxcomments',
 'south']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ajaxcomments/utils.py" in wrapped
  57.         return func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  76.                     response = view_func(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/views/decorators/http.py" in inner
  37.             return func(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/comments/views/comments.py" in post_comment
  127.         request = request
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
  162.             response = receiver(signal=self, sender=sender, **named)
File "/Users/emilepetrone/code/apprentice2/src/utils.py" in comment_email
  24.   print url
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py" in __repr__
  344.             u = unicode(self)

Exception Type: TypeError at /comments/post/
Exception Value: coercing to Unicode: need string or buffer, Want found

Here is the Want model:

     class Want(models.Model):

       pub_date =         models.DateTimeField(default=datetime.now,auto_now_add=True,db_index=True)
       body = models.TextField(default='',max_length=1000)
       body_html = models.TextField(editable=False, blank=True)

       #Metadata
       mentee = models.ForeignKey(User)
       points = models.IntegerField(default=3)
       enable_comments = models.BooleanField(default=True)
       featured = models.BooleanField(default=False)

       #Tags
       tags = TagField(help_text="Autocomplete")

       def get_tags(self):
         return Tag.objects.get_for_object(self)      

       class Meta:
         ordering = ['-pub_date']

       def __unicode__(self):
         return self

       def save(self):
         self.body_html = markdown(self.body)
         super(Want, self).save()

       def get_absolute_url(self):
         return ( { 'object_id': self.id })
       get_absolute_url = models.permalink(get_absolute_url)

Thank you for your help!

2
  • You'll need to post the full traceback. Commented Nov 21, 2010 at 10:49
  • Daniel, just added it in. Thanks Commented Nov 21, 2010 at 14:23

1 Answer 1

7

I think the problem is

def __unicode__(self):
    return self

which should return a unicode string rather than a "Want" instance. I'm not sure what you want there instead -- maybe "self.id"

Sign up to request clarification or add additional context in comments.

3 Comments

I thought of the want.get_absolute_url(), but no such luck. With the first guess, "self.id"- do you mean return self.id?
__unicode__() is supposed to return a unicode string, for example an empty string would even be valid, but its current return "self" is a Want instance not a unicode string. As for what specific string -- not knowing your code, or objectives -- I'm just guessing that "self.id" is a valid unicode string that would suit your purposes.
Yea i actually removed the def unicode and it works like a charm. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.