【问题标题】:can I use Django comment on other pages?我可以在其他页面上使用 Django 评论吗?
【发布时间】:2012-03-30 13:08:57
【问题描述】:

我想知道是否可以将 Django 内置的 cmets 框架用于其他与“博客条目”无关的页面。例如,我想在关于电影的每个页面上添加 cmets 字段。

我尝试按照https://docs.djangoproject.com/en/dev/ref/contrib/comments/ 此处的说明进行操作,但出现以下错误。我已经添加了必要的APP,并且做了syncdb。

Environment:


Request Method: GET
Request URL: http://localhost:8000/movie/603/

Django Version: 1.4
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'forms',
 'social_auth')
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 C:\xampp\htdocs\tddd27_project\templates\movie_info.html, error at line 3
   'comments' is not a valid tag library: Template library comments not found, tried django.templatetags.comments,django.contrib.staticfiles.templatetags.comments,django.contrib.admin.templatetags.comments,forms.templatetags.comments
   1 : {% extends "base.html" %}


   2 : {% load string_extras %}


   3 :  {% load comments %} 


   4 : {% block title %}{{ content.original_title }}{% endblock title %}


   5 : {% block content %}


   6 :  


   7 :  <h2>{{ content.original_title }} - ({{ content.release_date|year}})</h2>


   8 :  <table>


   9 :      <tr>


   10 :             <td><img src="http://cf2.imgobject.com/t/p/w185/{{ content.poster_path}}"></td>


   11 :             <td>{{ content.overview }}</td>


   12 :         </tr>


   13 : 


Traceback:
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\xampp\htdocs\tddd27_project\views.py" in movie_view
  65.   return render_to_response('movie_info.html',{'content':content}, RequestContext(request))
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\shortcuts\__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\loader.py" in render_to_string
  169.         t = get_template(template_name)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\loader.py" in get_template
  145.     template, origin = find_template(template_name)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\loader.py" in find_template
  134.             source, display_name = loader(name, dirs)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\loader.py" in __call__
  42.         return self.load_template(template_name, template_dirs)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\loader.py" in load_template
  48.             template = get_template_from_string(source, origin, template_name)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\loader.py" in get_template_from_string
  156.     return Template(source, origin, name)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\base.py" in __init__
  125.         self.nodelist = compile_string(template_string, origin)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\base.py" in compile_string
  153.     return parser.parse()
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\base.py" in parse
  267.                     compiled_result = compile_func(self, token)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\loader_tags.py" in do_extends
  214.     nodelist = parser.parse()
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\base.py" in parse
  267.                     compiled_result = compile_func(self, token)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\template\defaulttags.py" in load
  1043.                                           (taglib, e))

Exception Type: TemplateSyntaxError at /movie/603/
Exception Value: 'comments' is not a valid tag library: Template library comments not found, tried django.templatetags.comments,django.contrib.staticfiles.templatetags.comments,django.contrib.admin.templatetags.comments,forms.templatetags.comments

【问题讨论】:

  • 这应该适用于几乎任何模型,您能否发布您的模型和模板代码以便我们查看是否存在语法错误?

标签: django django-comments


【解决方案1】:

你能看出这里缺少什么吗?

Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'forms',
 'social_auth')

django.contrib.comments 似乎不在该列表中...

【讨论】:

  • 哦该死的,我盯着看了这么久,还以为django.contrib.messages是那个.... -_-;;;;;
  • Django 消息基本上会创建立即向用户显示的消息,即信息、成功、错误或警告,因此例如您上传了一张新照片并希望向用户显示它是成功的,您启动成功消息
猜你喜欢
  • 2014-12-11
  • 2013-03-03
  • 2021-03-17
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 2016-12-01
  • 2010-09-20
相关资源
最近更新 更多