在Django项目中,我试图将url传递给want实例。评论适用于旺旺。我一直试图弄清楚这个错误,但我很难过。
此功能:
def comment_email(request, comment, **kwargs):
want = get_object_or_404(Want, id=comment.object_pk)
url = want.get_absolute_url
print url
抛出此错误
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
以下是旺旺模型:
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)
感谢您的帮助!
答案 0 :(得分:7)
我认为问题是
def __unicode__(self):
return self
应返回unicode字符串而不是“Want”实例。我不确定你想要的是什么 - 也许是“self.id”