3

很抱歉发布与该错误相关的另一个问题:

'WSGIRequest' 对象没有属性 'find'

但我真的无法在任何地方找到答案。

我正在尝试使用django_model_comments应用程序,它扩展了 django 包含的评论应用程序。做了页面告诉的所有事情,但是在运行服务器时,我得到以下信息:

Environment:

Request Method: GET
Request URL: http://localhost:8000/feed/1

Django Version: 1.4.3
Python Version: 2.7.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'model_comments',
 'django.contrib.comments',
 'pinax_theme_bootstrap_account',
 'pinax_theme_bootstrap',
 'django_forms_bootstrap',
 'account',
 'metron',
 'user_app',
 'feed_app']
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 D:\Docs\Work\repo\project\feed_app\templates\feed.html, error at line 10
   'WSGIRequest' object has no attribute 'find'
  1 : {% load model_comment_tags %}
  2 : {% get_comment_form for feed as post_form %}             
  3 : {% render_comment_form post_form %}

Traceback:
File "D:\Docs\Work\repo\so_virtual_env\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "D:\Docs\Work\repo\project\feed_app\views.py" in get_user_feed
  37.                     'feed': private_feed})
File "D:\Docs\Work\repo\so_virtual_env\lib\site-packages\django\template\base.py" in render
  140.             return self._render(context)
File "D:\Docs\Work\repo\so_virtual_env\lib\site-packages\django\template\base.py" in _render
  134.         return self.nodelist.render(context)
File "D:\Docs\Work\repo\so_virtual_env\lib\site-packages\django\template\base.py" in render
  823.                 bit = self.render_node(node, context)
File "D:\Docs\Work\repo\so_virtual_env\lib\site-packages\django\template\debug.py" in render_node
  74.             return node.render(context)
File "D:\Docs\Work\repo\so_virtual_env\lib\site-packages\django\template\defaulttags.py" in render
  281.                 return nodelist.render(context)
File "D:\Docs\Work\repo\so_virtual_env\lib\site-packages\django\template\base.py" in render
  823.                 bit = self.render_node(node, context)
File "D:\Docs\Work\repo\so_virtual_env\lib\site-packages\django\template\debug.py" in render_node
  74.             return node.render(context)
File "D:\Docs\Work\repo\project\model_comments\templatetags\model_comment_tags.py" in render
  26.         return self.func(context)
File "D:\Docs\Work\repo\project\model_comments\templatetags\model_comment_tags.py" in wrap
  75.         form.set_request(request)
File "D:\Docs\Work\repo\project\model_comments\forms.py" in set_request
  106.             self.fields['from_url'].initial = unicode(Url(request))
File "D:\Docs\Work\repo\project\model_comments\url_util.py" in __init__
  11.         self.scheme, self.netloc, self.path, self.params, self.query_string, self.fragment = urlparse.urlparse(url)
File "C:\Python27\Lib\urlparse.py" in urlparse
  134.     tuple = urlsplit(url, scheme, allow_fragments)
File "C:\Python27\Lib\urlparse.py" in urlsplit
  173.     i = url.find(':')

Exception Type: AttributeError at /feed/1
Exception Value: 'WSGIRequest' object has no attribute 'find'

使用模板标签时会发生错误:

html = "{% load model_comment_tags %} \
        {% get_comment_form for feed as post_form %}\
        {% render_comment_form post_form %}"
t = template.Template(html)
html = t.render(RequestContext(request, {'feed': private_feed}))

我检查了我所有的中间件,应用程序的顺序,删除了 .pyc 文件,并在模板中进行了各种实验。

4

1 回答 1

2

django_model_comments 库中有一个错误,因为它们在这里传递 HttpRequest 对象,而不是 Url 类所期望的字符串因此它应该调用build_absolute_uri()请求对象上的方法,然后将字符串传递给 Url 类。

所以基本上替换

unicode(Url(request))

unicode(Url(request.build_absolute_uri()))
于 2013-02-10T04:57:43.570 回答