Parse a javascript file having django template variables

723 views
Skip to first unread message

Alessandro Candini

unread,
Nov 25, 2011, 6:25:51โ€ฏAM11/25/11
to django...@googlegroups.com
Hi everybody.

I have an application with the following structure:

STO
โ”œโ”€โ”€ __init__.py
โ”œโ”€โ”€ jsonopenlayers
โ”‚ โ”œโ”€โ”€ __init__.py
โ”‚ โ”œโ”€โ”€ models.py
โ”‚ โ”œโ”€โ”€ static
โ”‚ โ”‚ โ”œโ”€โ”€ css
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ STO.css
โ”‚ โ”‚ โ””โ”€โ”€ js
โ”‚ โ”‚ โ””โ”€โ”€ renderMaps.js
โ”‚ โ”œโ”€โ”€ tests.py
โ”‚ โ””โ”€โ”€ views.py
โ”œโ”€โ”€ manage.py
โ”œโ”€โ”€ settings.py
โ”œโ”€โ”€ templates
โ”‚ โ””โ”€โ”€ jsonopenlayers
โ”‚ โ””โ”€โ”€ index.html
โ””โ”€โ”€ urls.py

Static files are found, but I have the problem that inside renderMaps.js
I have some Django template tag to be interpreted.
How can I tell Django to parse also that, in addition to my index.html?

Thanks in advance.

--
Alessandro Candini
MEEO S.r.l.
Via Saragat 9
I-44122 Ferrara, Italy
Tel: +39 0532 1861501
Fax: +39 0532 1861637
http://www.meeo.it

========================================
"ATTENZIONE:le informazioni contenute in questo messaggio sono
da considerarsi confidenziali ed il loro utilizzo รจ riservato unicamente
al destinatario sopra indicato. Chi dovesse ricevere questo messaggio
per errore รจ tenuto ad informare il mittente ed a rimuoverlo
definitivamente da ogni supporto elettronico o cartaceo."

"WARNING:This message contains confidential and/or proprietary
information which may be subject to privilege or immunity and which
is intended for use of its addressee only. Should you receive this
message in error, you are kindly requested to inform the sender and
to definitively remove it from any paper or electronic format."

Tom Evans

unread,
Nov 25, 2011, 9:06:35โ€ฏAM11/25/11
to django...@googlegroups.com

The only way you can manage this is to deliver that file not as a
static file, but as something that has been passed through the
template engine. The template engine can generate any content, be sure
to pass the correct content type ('text/javascript') when you return
the response, and use the cache_page decorator to avoid re-doing it
lots of times.

That is a pain, so a common work-around I use is to re-work it so that
the url to use is something that is passed to the function, and then
insert it into the appropriate templates.

Cheers

Tom

Alessandro Candini

unread,
Nov 25, 2011, 10:12:55โ€ฏAM11/25/11
to django...@googlegroups.com
Thank you, but I adopted the method indicated here:
http://stackoverflow.com/questions/6008908/use-django-template-tags-in-jquery-javascript

I have deleted renderMaps.js and created a new template named
index_renderMaps.html, containing only a script tag with inside the
javascript previously into renderMaps.js.
After that I have simply used an {% include "index_renderMaps.html" %}
into index.html template.

IanKRolfe

unread,
Nov 25, 2011, 10:14:04โ€ฏAM11/25/11
to Django users
Or you can render the js file as if it was a template.
Put the javascript in a template directory.

In the main HTML, change the tag to something like <script href='/
myapp/jscript/renderMaps/'>

In your "urls.py" add something like

('/myapp/jscript/renderMaps','myapp.views.rendermap')

then in your views.py create a view:

def rendermap(request):
# create the variables you need here
var1 = "Hello"
address = Address.objects.get(id=1)
:
return render_to_response('renderMaps.js', locals(),
RequestContext(request))

You can see the idea, anyhow!

> Fax: +39 0532 1861637http://www.meeo.it

Reply all
Reply to author
Forward
0 new messages