Use Django template tags in jQuery/Javascript?

Can I use Django's template tags in Javascript? Like using {% form.as_p %} in jQuery to dynamically add forms to the page.

-------------Problems Reply------------

Yes, I do it frequently. Your javascript has to be served through django, but if you just have it in the html header as inline javascript you'll be fine.

E.g: I use this to put prefix on a dynamic formset I use.

{% extends "base.html" %}
{% block extrahead %}
<script type="text/javascript">
$(document).ready(function() {
{# Append fields for dynamic formset to work#}
{% for fset, cap, _, tid in study_formsets.fset_cap_tid %}
$(function() {
$('.form_container_{{ tid }}').formset({
prefix: '{{ fset.prefix }}',
formCssClass: '{{ tid }}',
extraClasses: ['myrow1', 'myrow2']
});
});
{% endfor %}
});
</script>
{% endblock %}

Note in "base.html" I have a html head where the jquery libraries are loaded, that contains {% block extrahead %}{% endblock %}.

You can't use Django's template tags from your Javascript code if that's what you mean. All the Django variables and logic stop existing after the template has been rendered and the HttpResponse has been sent to the client. At that moment when Javascript executes, the client (browser) has no notion the variables you rendered the template with (such as "form").

What you can do is have Javascript modify your HTML page using chunks of HTML that were rendered by your Django template.

If you want to generate HTML on client side, I'd recommend to look at client side tempalte libraries (eg. JQuery Templates - use those with the {% verbatim %} templatetag).

If you want to use variables inside your rendered javascript I (that's my opnion), think it's a bad idea. But if all you want is to generate URL for your views, media and static files, I do this a lot.

Take a look to this github: jscssmin

Category:javascript Views:0 Time:2011-05-15

Related post

  • Append Django template tag with Jquery 2011-05-01

    I want to build a menu where I can set one link highlighted using the {% block %} tag. I have something like this in my Javascript: <loop> $('#a-div').append('{% block ' + variable + ' %} <a href...</a> {% endblock %}') <endloop>

  • using django template tags inside external JavaScript 2012-02-18

    i have included a js file(eg:application.js) inside my html page but i couldn't able to use django template tag inside that js file ,is there any way to use django template tag directly inside external js file? --------------Solutions------------- On

  • Append Django template tag in jquery 2010-08-31

    <script> $("#tableview").css({'display' : block}); $("#tableview").append('{{html}}'); or $("#tableview").html('{{html}}'); </script> <div id="tableview" style="display:none;"></div> the {{html}} i.e, generated on the server g

  • django/jquery - how do you mingle jquery template tags with django template tags without conflicts 2011-06-18

    How do I use django templates tags with jquery template tags (link) without creating any conflicts? Both use {{ }} for marking tags. Does Django 1.3 have something to deal with this kind of issue? Real examples would be truly appreciated. -----------

  • Adding Google Map Markers with DJango Template Tags in Javascript 2012-01-13

    I am trying to add a bunch of map markers to a map using Django template tag variables as the latitude and longitude but things aren't working too well. Here is what I have in the view javascript code. I load the code in as an external JS file. Here

  • Django template tags in JavaScript - invalid syntax without quotes, invalid syntax with quotes 2010-11-11

    It is possible you have Javascript read vars from Django template tags like var spec = "{{ foo }}";. However, if foo needs to be a JSON object. it becomes like this: var spec = "{"2": {"guid": 2, "contentBlocks": {"2_1": {"guid": "2_1", "type": "list

  • How to use Django template tags and filters with jQuery? 2011-05-06

    The problem in general is this: I have a page with content and user can comment it. Inserting comment should happen without reloading the page. Commenting form gets handled in a view which saves the comment to database. After saving the comment I ser

  • Assign JavaScript value to Django template tag argument 2011-06-30

    How do I assign an argument value to a Django template tag, using JavaScript? {% url path.to.some_view arg=v %} This doesn't work: <script> var v = 5; </script> {% url path.to.some_view arg=v %} --------------Solutions------------- This s

  • Modify django template variable in Jquery code 2012-01-24

    I have django template where i'm checking variable for TRUE or FALSE. Now on specific event in Jquery i want to modify this django variable value to either TRUE or FALSE. in views.py var = True in Template {% if var %} do something {% endif %} HTML c

  • Django Template Tag inside JS script 2010-10-28

    I'm new to javascript so bear with me: I'm implementing Google Maps API and I'd like the first marker's InfoWindow to open when the template is first rendered but ONLY if certain condition is true. so, I have something like this: {% if project %} //t

  • Syntax Errors in my JS caused by Django Template tags 2014-03-25

    I'm using pycharm on a Django project. In my templates, I have some embedded JS. And in a couple places, I'm using Django template tags in the JS. And that is causing PyCharm to mark all the JS after these template tags as a syntax error. I know it c

  • Custom Django template tag to look up a string username and return it as a user object 2010-01-06

    I use some third-party template tags in my Django Application (maintained elsewhere) that return a username as a string I can access in my templates like this. {% for user in gr.user.foll.list %} {{user}} Trouble is because {{user}} is returned as a

  • Add Different Style to list tag using jquery/javascript 2010-09-10

    Is there any ways to add different style class to all li in a ul tag using jquery/javascript/php. Consider I have a list as follows <ul><li>a</li><li>b</li><li>c</li><li>d</li><li>e</li

  • How do I write a Django template tag for access control? 2010-09-22

    I'm trying, in vain, to create a simple Django template tag to either show or hide a "delete" link next to a submitted comment on my site. In a nutshell, I want to pass the comment object to the template tag, determine if the currently logged in user

  • Jinja2 templates using Django template tags 2011-01-25

    I'm using Jinja2 on a new project, but would like to use the django-socialregistration app, which relies on Django template tags. Jinja2 doesn't play nicely with template tags, so I'm wondering if there's a quick workaround? Template tags: {% load fa

  • calling python function from django template tags 2011-03-08

    How can we pass a variable from a django template tag i.e, i want to do something like this {{emp.get_names('a')}} emp is the object that i am passing from my views class Emp(models.Model): name = models.CharField(max_length=255, unique=True) address

  • How can you use Django template tags in a Jinja2 template? 2011-06-18

    There are a number of off-the-shelf Django packages that offer template tags, and I'd like to use them in my project despite the fact that it uses Jinja2. I've seen some workarounds that allow other template engines to use Django tags. These involve

  • How can I pass objects to a custom django template tag? I figured out a way, but it seems hackish 2011-06-19

    OK. I am writing a system where users can "pick the winner". I have a "matchup" table and a "picks" table. On the matchup webpage, I load all of the matchups for that day into the context along with all of the picks for that day. Now, I need to displ

  • Jump to matching django template tag in emacs? 2011-06-25

    Emacs has a feature that allows one to jump to matching HTML tags. You position your cursor on an opening <div>, say, then press C-c <right arrow>, and the cursor jumps to the corresponding closing </div> tag. Is there any equivalen

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.076 (s). 11 q(s)