0

I have a Django template base.html:

// inside the javascript
var this_page = "{{ this_page }}";

// inside the HTML part
{% ifnotequal this_page "home" %}
...Some HTML...
{% endifnotequal %}

The part inside the javascript area is incorrect. Actually, Firebug shows me in the HTML tab:

var this_page = "home";

but if I look into the GET inside the Console tab, Firebug shows:

var this_page = "about";

which would be correct, but debugging shows that 'this_page' is set to 'home'. I also tried it:

{% ifnotequal this_page "home" %}
do some javascript
{% endifnotequal %}

but that did also not work correctly.

What am I doing wrong?

EDIT: This problem only occurs when I access this page via a link. If I access the "about" page directly, I do not have this problem.

Testing on Ubuntu, Firefox 6.0.2, on 'runserver'

Carl K.
  • 457
  • 4
  • 16
  • 1
    Are you saying `{{ this_page }}` outputs "home" but `{% ifequal this_page "home" %}foo{% endifequal %}` does not print foo? Is the html and javascript in the same file? Are you 100% certain the variables are being passed into these templates? – Yuji 'Tomita' Tomita Mar 06 '12 at 04:26
  • @Yuji: Yes, javascript section and html are in one file. I have found out that on the "about"-page if I have `{% ifequal this_page "home" %}foo{% endifequal %}` inside the javascript section, "foo" is printed (incorrect), while inside the html section, it is not (correct). I assume, that this has more something to do with the browser, since this makes no sense. But I have no idea. – Carl K. Mar 06 '12 at 09:38

2 Answers2

4

JavaScript is a client-side language. The HTML of your template is rendered by the python on server side so you cannot change rendered variable's value through java script code. In your case this_page is rendered html variable and var this_page = "{{ this_page }}"; defines a separate javascript variable and does not change the rendered html this_page variable. So firebug is correct.

You can use Ajax for this purpose if you want to dynamically change the rendered HTML.

Zubair Afzal
  • 2,016
  • 20
  • 29
  • On the server side, I put {"this_page" : "about"} into the templage tags, so whatever the left hand side will be, the right hand should be "about". No? – Carl K. Mar 06 '12 at 09:26
  • thanks, your answer was going into the right direction. The problem lied with using jquery mobile, which did not reload the head as I expected. I would give a +1, but I can't. – Carl K. Mar 07 '12 at 05:43
0

The solution to the problem had (perhaps not surprisingly) nothing to do with the question. Apologies that I asked into the wrong direction.

Since I use jquery mobile, it behaved not as I expected: the body content (per default) is loaded per ajax on page change, while the head part is only loaded once.

The solution to the problem I have found here.

Community
  • 1
  • 1
Carl K.
  • 457
  • 4
  • 16