开发者

weird django-template-jquery-javascript shenanigans!

开发者 https://www.devze.com 2023-02-27 06:06 出处:网络
I have the following: return render_to_response(\'a.html\', {\'b\': 3}, context_instance=...) in a.html:

I have the following:

return render_to_response('a.html', {'b': 3}, context_instance=...)

in a.html:

{{ b }}

outputs 3

{% url app.views.something b %}

gives me:

/something/3/

but

<script type="text/javascript">
    $(document).ready(function() {
        alert('{{ b }}');
    });
</script>

pops up an alert dialog with 3. But if you view the source you get:

<script type="text/javascript">
    $(document).ready(function() {
        alert('0');
    });
</script>

If I try to use {{ b }} in jquery anywhere else it just keeps returning me '0'.

The same happens if I do the following and check the source before the popup arrives

<script type="text/javascript">
    setTimeout("alert('{{ b }}')",2000);
<script>

The popup says '3' but the source says '0'.

Has anyone ha开发者_StackOverflow社区d anything like this before?

EDIT:

I have used HTTP fox to deduce that this is an AJAX problem. Django is returning '3' to the browser.

While the example code I have provide above is an over simplification. What I am basically doing is refreshing the div (content_ct's parent) the code is in using a jquery ajax command.

$('#ctSelection').change(function() {

    if($('#ctSelection option:selected').val()=='1') {
        $.get('/company/ct/1/', function(data) {
            $('#content_ct').parent().html(data);
        });
    }

    if($('#ctSelection option:selected').val()=='2') {
        $.get('/company/ct/2/', function(data) {
            $('#content_ct').parent().html(data);
        });
    }

});

EDIT

I believe this is due to jquery overlapping handlers and always using the first one in the stack


Is that a typo in your javascript closing tag, it should be:

</script>

instead of

<script>

Other than that, I tried your javascript with jQuery 1.5 and Django 1.3, it's working fine.


I haven't fond a solution for the problem but because django is returning the right code it must be a problem with jquery which I have expanded upon here.

EDIT I finally switched frameworks to dojo because I couldn't find away round this problem

0

精彩评论

暂无评论...
验证码 换一张
取 消