0

I'm sure I'm missing something basic here, but I've spent hours trying to wrap my head around this. I'm very new to Django, so please forgive me:

How do I call a method from a template?

Specifically, i would like to load a page that autoplays and element, then have it load / play another one with a different file. (Chosen via a python script.)

Do I need to define this as a custom tag? What concept am I missing here?

Thanks so much everyone! Loren

arie
  • 18,737
  • 5
  • 70
  • 76
Sauce McBoss
  • 6,567
  • 3
  • 20
  • 22

2 Answers2

2

Yes, you can't call functions (with arguments) in a template. That's what templatetags are for.

You could of course swap the template engine to a more permissive one, let's say, Jinja2. But i guess that solution is too complex for your limited task.

If you know your playlist in advance, you could construct it in your view and pass it to your template.

But using python to play the media and do the redirection is probably not a good idea, if possible in a sensible way at all.

As DTing mentioned in his comment this sounds like typical use case for Javascript/JQuery. There are many JQuery-Plugins out there. Finding one that's suits your purpose shouldn't be too hard: Take a look at jPlayer for example.

And if you want to get your hands dirty take a look at this article: Building a Custom HTML5 Audio Player with jQuery

arie
  • 18,737
  • 5
  • 70
  • 76
  • Thanks for the advice -- but I now realize that I failed to fully explain myself. I want to have the onended="" call of the – Sauce McBoss Apr 15 '11 at 22:23
  • ...At the moment, I have it calling a "audioHasFinishedPlaying();" js function, which works fine. The problem is, I can't figure out what the function should do after that. Should it call a custom template tag? If so, how should the custom tag replace the src="" field? – Sauce McBoss Apr 15 '11 at 22:43
  • Still not sure if you need a template tag. As i said, can't you pass the list of possible tunes with the view that renders the page? From there you should be able to do everything using js. (Though you could of course load a dynamically generated playlist via Ajax-Calls) See this answer for a solution how to control the src of your player: http://stackoverflow.com/questions/2551859/html-5-video-or-audio-playlist/2552131#2552131 The only difference is that you have to grab your next tune from a list/array. – arie Apr 16 '11 at 08:54
1

I think you might be a little confused as to how templates in Django work. A template is responsible for producing a string on the server. This string is full of HTML markup. A template tag is used to render portions of a string based on server data.

Once this string has been constructed, it is sent via the web server to the client. Remember, this is only a string.

From this point on, any code that needs to execute must be in javascript. Your example refers to waiting for a file to finish playing on the client - therefore the client (javascript) is responsible for waiting for that file to finish, and starting a new one. You can NOT call python code from the client without making another HTTP request to the server. Template Tags do not traverse HTTP to the client. They are simply a way of helping the server to construct the string that is sent.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • Would something like Dajax do the trick? http://www.dajaxproject.com/ (I'm fairly new to web development -- just want to be sure that I'm trying to learn the right things...) Thanks! – Sauce McBoss Apr 17 '11 at 05:45
  • @Irog, honestly, I've not used dajax before so I don't know. What you want to do, is have a django view that returns a representation of your data in html or json, call that view from javascript using jquery.get() (AJAX), and use the result of that http request to do your client side processing. Perhaps ask a new question for advice on how to do this. – Josh Smeaton Apr 17 '11 at 10:03
  • Thanks so much! I think Dajax does exactly that. (Though I may be wrong.. Will need to do more learning..) Thanks so much! – Sauce McBoss Apr 18 '11 at 04:12