Parsing XML in the template? (New to Django and Python)

809 views
Skip to first unread message

sleepjunk

unread,
Dec 5, 2009, 8:16:48 PM12/5/09
to Django users
Hello :) I am working on a small video site that allows a user to add
a video to the site by submitting a Vimeo.com or Youtube.com URL. I'm
trying to display thumbnails without hosting them. For Vimeo videos I
need to parse an XML file to find the thumbnail location.

On my homepage I would like to display the 5 newest videos. As of now
I'm displaying every video in the database on the homepage. Each video
will have a different thumbnail and XML file to parse. As of right now
my "main_page" in my views.py file looks like this:

def home_page(request):
template = get_template('home_page.html')
video = Video.objects.all()
feed = urllib.urlopen("http://vimeo.com/api/v2/video/7875440.xml")
tree = ET.parse(feed)
root = tree.getroot()
vimeo_thumbnail = root.findtext("video/thumbnail_medium")
variables = Context({
'video': video,
'vimeo_thumbnail': vimeo_thumbnail
})
output = template.render(variables)
return HttpResponse(output)

How can I parse a different XML file for every video? Thank you much.
Again, I am new and could be going about this completely wrong and
gladly accept any feedback you have.

Thank you,
Matt

Sam Lai

unread,
Dec 5, 2009, 10:07:11 PM12/5/09
to django-users
See inline.

2009/12/6 sleepjunk <slee...@gmail.com>:
> Hello :) I am working on a small video site that allows a user to add
> a video to the site by submitting a Vimeo.com or Youtube.com URL. I'm
> trying to display thumbnails without hosting them. For Vimeo videos I
> need to parse an XML file to find the thumbnail location.
>
> On my homepage I would like to display the 5 newest videos. As of now
> I'm displaying every video in the database on the homepage. Each video
> will have a different thumbnail and XML file to parse. As of right now
> my "main_page" in my views.py file looks like this:
>
> def home_page(request):
>        template = get_template('home_page.html')
>        videos = Video.objects.all()

videos_context = [ ]
for v in videos:
>         feed = urllib.urlopen("http://vimeo.com/api/v2/video/7875440.xml")
>         tree = ET.parse(feed)
>         root = tree.getroot()
>         vimeo_thumbnail = root.findtext("video/thumbnail_medium")
videos_context.append( { 'video' : v, 'thumbnail_url' :
vimeo_thumbnail } )

>        variables = Context({
>                'videos' : videos_context,
>        })
>        output = template.render(variables)
>        return HttpResponse(output)
>
> How can I parse a different XML file for every video? Thank you much.
> Again, I am new and could be going about this completely wrong and
> gladly accept any feedback you have.

You could try something like the modified view above. It loops through
every video, gets the thumbnail URL, creates an object containing the
video model instance and the thumbnail URL, and adds it to the list
videos_context.

You could then access them in your template like this (assuming
there's a name field in your Video model):

{% for v in videos %}
<img src="{{v.thumbnail_url}}">{{ v.video.name }}
{% endfor %}

The alternate way of doing this is to make a tag filter, see
http://docs.djangoproject.com/en/dev/howto/custom-template-tags/

HTH,

Sam

sleepjunk

unread,
Dec 5, 2009, 11:16:02 PM12/5/09
to Django users
Thank you for the response. I don't believe I explained well enough
exactly what I'm trying to do.

I have table Video with a field vimeo_code. Each vimeo_code has its
own XML file. The XML file I used in my views.py is just a random
video I saw on vimeo. I need to replace the vimeo_code so the XML file
I'm parsing looks like http://vimeo.com/api/v2/video/vimeo_code.xml

Would it be a better idea to do this as a tag filter? Any additional
information for me if I do it that way?

Thanks again, I appreciate your help.
Matt

On Dec 5, 10:07 pm, Sam Lai <samuel....@gmail.com> wrote:
> See inline.
>
> 2009/12/6 sleepjunk <sleepj...@gmail.com>:

Sam Lai

unread,
Dec 5, 2009, 11:40:30 PM12/5/09
to django...@googlegroups.com
Inline.

2009/12/6 sleepjunk <slee...@gmail.com>:
> Thank you for the response. I don't believe I explained well enough
> exactly what I'm trying to do.
>
> I have table Video with a field vimeo_code. Each vimeo_code has its
> own XML file. The XML file I used in my views.py is just a random
> video I saw on vimeo. I need to replace the vimeo_code so the XML file
> I'm parsing looks like http://vimeo.com/api/v2/video/vimeo_code.xml

Ok, so just replace this line:
with,

feed =
urllib.urlopen("http://vimeo.com/api/v2/video/%s.xml" % v.vimeo_code)

This replaces the code in the URL with the current vimeo_code in the loop.

I think we're on the same page; give it a shot and see how it goes.

> Would it be a better idea to do this as a tag filter? Any additional
> information for me if I do it that way?

I'm sure someone will have a more concrete reason for doing it one way
or the other. Personally though, it's a matter of style. I prefer
having it in the view function because then I can see immediately that
extra http calls will be made when looking through my code. If I used
a tag filter, I would have to look at the view function to find the
template, then the template to see if custom tag filters were used,
then the .py files for the tag filters to see what it's doing.

Tag filters are pretty simple things; the Django docs link I gave
should be easy enough. Feel free to post questions if you get stuck
though.
> --
>
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>
>

sleepjunk

unread,
Dec 6, 2009, 1:36:03 PM12/6/09
to Django users
Thanks so much for your help. I'm sorry to ask another question, but I
am getting an error and have spent a few hours trying to figure it
out. Googling has not helped me.

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.1.1
Python Version: 2.6.4
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'discvideos.videos']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
site-packages/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args,
**callback_kwargs)
File "/Users/matt/Sites/discvideos/../discvideos/videos/views.py" in
home_page
21. tree = ET.parse(feed)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
xml/etree/ElementTree.py" in parse
862. tree.parse(source, parser)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
xml/etree/ElementTree.py" in parse
586. parser.feed(data)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
xml/etree/ElementTree.py" in feed
1245. self._parser.Parse(data, 0)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
xml/etree/ElementTree.py" in _default
1201. self._parser.ErrorColumnNumber)

Exception Type: ExpatError at /
Exception Value: undefined entity &hellip;: line 198, column 55


Here is my home_page: http://utilitybase.com/paste/23116

http://diveintopython3.org/xml.html seems to be explaining the problem
but I haven't gotten the code there to work for me yet. Thank you.

On Dec 5, 11:40 pm, Sam Lai <samuel....@gmail.com> wrote:
> Inline.
>
> 2009/12/6 sleepjunk <sleepj...@gmail.com>:
>
> > Thank you for the response. I don't believe I explained well enough
> > exactly what I'm trying to do.
>
> > I have table Video with a field vimeo_code. Each vimeo_code has its
> > own XML file. The XML file I used in my views.py is just a random
> > video I saw on vimeo. I need to replace the vimeo_code so the XML file
> > I'm parsing looks likehttp://vimeo.com/api/v2/video/vimeo_code.xml

Sam Lai

unread,
Dec 6, 2009, 9:46:29 PM12/6/09
to django...@googlegroups.com
2009/12/7 sleepjunk <slee...@gmail.com>:
Try adding this before line 5 in your the code you linked to:

parser = ET.XMLParser()
parser.entity['hellip'] = ""

Then change,

tree = ET.parse(feed)

to,

tree = ET.parse(feed, parser)

This will ignore all instances of the &hellip; entity that exists only
in HTML, and not XML. You can replace it with another character if you
want; just replace "" with something else.

Docs for XMLParser here -
http://effbot.org/elementtree/elementtree-xmlparser.htm

Also saw that your context doesn't include videos_context. It needs
to, otherwise all the work you just did would be inaccessible in the
template.
Reply all
Reply to author
Forward
0 new messages