AttributeError:"str"对象没有属性"_meta" [英] AttributeError: 'str' object has no attribute '_meta'

查看:134
本文介绍了AttributeError:"str"对象没有属性"_meta"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Django,今天来到评论部分,我只是从Django文档开始练习.https://docs.djangoproject.com/en/1.4/ref/contrib/comments/example/.当我在DetailView页面上练习时,添加注释表单的命令效果很好 但是现在我也想向ListView页面添加评论表单,然后出现此错误.

I just begin to study Django, and today come to the comment part, I just practice from the Django Document.https://docs.djangoproject.com/en/1.4/ref/contrib/comments/example/. The command to add comment form works well when i do my practice on DetailView page but now I also want to add a comment form to ListView page then it got this error.

以下是回溯: 环境: 请求方法:GET

below is the traceback: Environment: Request Method: GET

Django Version: 1.4.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'books',
'django.contrib.comments')
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')


Template error:
In template /home/ryu/emacs/emacs-code/djcode/mysite2/templates/books/publisher_list.html, error at line 19
'str' object has no attribute '_meta'


9 :   <th> website</th>


10 : </tr>


11 : {% for publisher in object_list %}


12 : <tr>


13 :   <th><a href="/mysite2/publishers/{{publisher.id}}">{{ publisher.name }}</a></th>  


14 :   <th>{{publisher.country}}</th>


15 :   <th>{{publisher.website}}</th>


16 : </tr> 


17 : {% endfor %}


18 : </table>


19 :  {% render_comment_list for books.publisher %} 


20 : {% endblock %}


21 : 
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
136.                     response = response.render()
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render
104.             self._set_content(self.rendered_content)
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in       rendered_content
81.         content = template.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
140.             return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
123.         return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
123.         return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74.             return 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/base.py" in render
823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-          packages/django/contrib/comments/templatetags/comments.py" in render
201.         ctype, object_pk = self.get_target_ctype_pk(context)
File "/usr/local/lib/python2.7/dist- packages/django/contrib/comments/templatetags/comments.py" in get_target_ctype_pk
100.             return ContentType.objects.get_for_model(obj), obj.pk
File "/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/models.py" in get_for_model
32.         opts = self._get_opts(model)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/models.py" in   _get_opts
20.         return model._meta.concrete_model._meta

Exception Type: AttributeError at /mysite2/publishers/
Exception Value: 'str' object has no attribute '_meta'

现在我想知道如何使用内置注释在每个页面上添加注释?

now I want to know how could i add every page a comment using the build-in comments??

推荐答案

然后您要说的是将appname.modelname传递给模板标签,实际上应该将模型的实例传递给模板标签,例如:

What you are saying then is that you pass appname.modelname to the template tag, you should actually pass an instance of the model to the template tag something like:

{% render_comment_list for publisher %}

如果发布者是发布者模型的实例,因为它看起来像在您的代码中一样.或者:

if publisher is an instance of the Publisher model as it looks to be in your code. Or alternatively:

{% render_comment_list for books.publisher publisher.id %}

就像上面提到的psjinx.

as psjinx mentioned above.

这篇关于AttributeError:"str"对象没有属性"_meta"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆