3

Sorry to post yet another question related to the error:

'WSGIRequest' object has no attribute 'find'

But I really can't find the answer anywhere.

I'm trying to use the django_model_comments app, which extends django's included comment app. Did everything the page tells, however when running the server, I get the following:

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'

And the error happens when a templatetag is used:

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}))

I've checked all my middleware, the order of apps, deleted .pyc files, and made all sorts of experiments in the template.

Anoyz
  • 7,431
  • 3
  • 30
  • 35
  • Please post the code for the model_comment_tags tag library. – Paulo Feb 04 '13 at 03:01
  • There's too much code, and even that code is not the final code (it eventually calls functions from forms.py, and so on). Nonetheless, here is the code: https://github.com/twig/django_model_comments/blob/master/templatetags/model_comment_tags.py – Anoyz Feb 04 '13 at 17:18
  • Are you using the Url class provided in the docs ? – Paulo Feb 06 '13 at 06:56
  • Yes, I created a separated file called url_utils.py, where I placed the Url class he made, which I then import in all files where it is used. – Anoyz Feb 07 '13 at 10:31
  • 1
    If that's true then there's a bug in the django_model_comments library, because here [link](https://github.com/twig/django_model_comments/blob/master/forms.py#L106) they pass the HttpRequest object not a string which is what the Url class is expecting, so instead it should call the build_absolute_uri() method on the request object and then pass the string to the Url class. – Paulo Feb 09 '13 at 01:41
  • yeah! That worked. Care to make a proper answer so I can set it as the correct one? – Anoyz Feb 09 '13 at 16:38

1 Answers1

2

There's a bug in the django_model_comments library, because here they pass the HttpRequest object, not a string which is what the Url class here is expecting, so instead it should call the build_absolute_uri() method on the request object and then pass the string to the Url class.

So basically replace

unicode(Url(request))

with

unicode(Url(request.build_absolute_uri()))
Paulo
  • 6,982
  • 7
  • 42
  • 56