I'm writing a basic RSS feed reader in Django. I have a form in which a user submits a rss feed, and I add it to his feeds list. But for some reason, I'm unable to extract basic information about the feed using feed parser.
when i run the following code:
def form_valid(self, form):
user = self.request.user
link = form.cleaned_data['link']
feed = feedparser.parse(link).feed
title = feed.title
try:
feed_obj = Feed.objects.get(link=link)
except ObjectDoesNotExist:
feed_obj = Feed(link=link, title=title)
feed_obj.save()
user.get_profile().feeds.add(feed_obj)
return super(DashboardView, self).form_valid(form)
Django throws me an "object has no attribute 'title'" exception on line 5:
title = feed.title
Full error details are:
Traceback:
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
25. return view_func(request, *args, **kwargs)
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
86. return handler(request, *args, **kwargs)
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
165. return self.form_valid(form)
File "/home/yaniv/nextfeed/profiles/views.py" in form_valid
48. title = feed.title
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/feedparser.py" in __getattr__
416. raise AttributeError, "object has no attribute '%s'" % key
Exception Type: AttributeError at /dashboard
Exception Value: object has no attribute 'title'
What am I doing wrong?
EDIT: I traced the program with pdb. Right before the problematic line, I got:
(Pdb) link
u'http://feedparser.org/docs/examples/rss20.xml'
(Pdb) feed
{'xhtml_script': {'type': u'text/javascript', 'language': u'javascript'}, 'summary': u''}
It's been a while since I used feedparser, but IIRC, the parser returns a dictionary, like so:
You seem to have gotten an object back from foo.feed, but that's not what you want.