if statement in html (with jinja2) - where should I edit to make it work?











up vote
1
down vote

favorite












UPDATE : There was whitespaces in session['sid'] and that was why this didn't work!



I'm using python, flask, jinja2, html.
This is a part of my html code.



    <h1>{{session['sid']}}</h1>                   <!--shows 'admin'-->
<ul>
{% if session['sid'] != 'admin' %}
<li> <a href="/sendmail">SEND MAIL</a> </li>
<li> <a href="">MAIL BOX</a> </li>
{% endif %}


session is a dictionary type data sent to this html from python and I double checked that session['sid'] is 'admin'. But those hyperlinks still show up, so it looks like if statements doesn't work. Somewhere in the code, there is also session['sid']!='admin' which also doesn't work. I googled about ifstatement example in html, but I don't still understand where I made mistake. Can anybody let me know what I am missing here?



I have tried excluding ' around admin or using {% if equal session['sid'] 'admin'%} instead.










share|improve this question
























  • what error you are getting?
    – Harsha B
    Nov 12 at 6:18










  • why not just debug the HTML? Inserting {{ session['sid'] }} will display its value and you can then determine whether it is or is not equal to 'admin'
    – Attack68
    Nov 12 at 7:00










  • @Attack68 edited my code.
    – PuffedRiceCrackers
    Nov 12 at 7:23










  • @HarshaB There's no error. Just the if statement doesn't work.
    – PuffedRiceCrackers
    Nov 12 at 7:26










  • Can you verify type(session['sid']) is a str, just in case it's not another type that just stringifies to admin?
    – Amadan
    Nov 12 at 7:37















up vote
1
down vote

favorite












UPDATE : There was whitespaces in session['sid'] and that was why this didn't work!



I'm using python, flask, jinja2, html.
This is a part of my html code.



    <h1>{{session['sid']}}</h1>                   <!--shows 'admin'-->
<ul>
{% if session['sid'] != 'admin' %}
<li> <a href="/sendmail">SEND MAIL</a> </li>
<li> <a href="">MAIL BOX</a> </li>
{% endif %}


session is a dictionary type data sent to this html from python and I double checked that session['sid'] is 'admin'. But those hyperlinks still show up, so it looks like if statements doesn't work. Somewhere in the code, there is also session['sid']!='admin' which also doesn't work. I googled about ifstatement example in html, but I don't still understand where I made mistake. Can anybody let me know what I am missing here?



I have tried excluding ' around admin or using {% if equal session['sid'] 'admin'%} instead.










share|improve this question
























  • what error you are getting?
    – Harsha B
    Nov 12 at 6:18










  • why not just debug the HTML? Inserting {{ session['sid'] }} will display its value and you can then determine whether it is or is not equal to 'admin'
    – Attack68
    Nov 12 at 7:00










  • @Attack68 edited my code.
    – PuffedRiceCrackers
    Nov 12 at 7:23










  • @HarshaB There's no error. Just the if statement doesn't work.
    – PuffedRiceCrackers
    Nov 12 at 7:26










  • Can you verify type(session['sid']) is a str, just in case it's not another type that just stringifies to admin?
    – Amadan
    Nov 12 at 7:37













up vote
1
down vote

favorite









up vote
1
down vote

favorite











UPDATE : There was whitespaces in session['sid'] and that was why this didn't work!



I'm using python, flask, jinja2, html.
This is a part of my html code.



    <h1>{{session['sid']}}</h1>                   <!--shows 'admin'-->
<ul>
{% if session['sid'] != 'admin' %}
<li> <a href="/sendmail">SEND MAIL</a> </li>
<li> <a href="">MAIL BOX</a> </li>
{% endif %}


session is a dictionary type data sent to this html from python and I double checked that session['sid'] is 'admin'. But those hyperlinks still show up, so it looks like if statements doesn't work. Somewhere in the code, there is also session['sid']!='admin' which also doesn't work. I googled about ifstatement example in html, but I don't still understand where I made mistake. Can anybody let me know what I am missing here?



I have tried excluding ' around admin or using {% if equal session['sid'] 'admin'%} instead.










share|improve this question















UPDATE : There was whitespaces in session['sid'] and that was why this didn't work!



I'm using python, flask, jinja2, html.
This is a part of my html code.



    <h1>{{session['sid']}}</h1>                   <!--shows 'admin'-->
<ul>
{% if session['sid'] != 'admin' %}
<li> <a href="/sendmail">SEND MAIL</a> </li>
<li> <a href="">MAIL BOX</a> </li>
{% endif %}


session is a dictionary type data sent to this html from python and I double checked that session['sid'] is 'admin'. But those hyperlinks still show up, so it looks like if statements doesn't work. Somewhere in the code, there is also session['sid']!='admin' which also doesn't work. I googled about ifstatement example in html, but I don't still understand where I made mistake. Can anybody let me know what I am missing here?



I have tried excluding ' around admin or using {% if equal session['sid'] 'admin'%} instead.







python html flask jinja2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 13:22

























asked Nov 12 at 5:55









PuffedRiceCrackers

466




466












  • what error you are getting?
    – Harsha B
    Nov 12 at 6:18










  • why not just debug the HTML? Inserting {{ session['sid'] }} will display its value and you can then determine whether it is or is not equal to 'admin'
    – Attack68
    Nov 12 at 7:00










  • @Attack68 edited my code.
    – PuffedRiceCrackers
    Nov 12 at 7:23










  • @HarshaB There's no error. Just the if statement doesn't work.
    – PuffedRiceCrackers
    Nov 12 at 7:26










  • Can you verify type(session['sid']) is a str, just in case it's not another type that just stringifies to admin?
    – Amadan
    Nov 12 at 7:37


















  • what error you are getting?
    – Harsha B
    Nov 12 at 6:18










  • why not just debug the HTML? Inserting {{ session['sid'] }} will display its value and you can then determine whether it is or is not equal to 'admin'
    – Attack68
    Nov 12 at 7:00










  • @Attack68 edited my code.
    – PuffedRiceCrackers
    Nov 12 at 7:23










  • @HarshaB There's no error. Just the if statement doesn't work.
    – PuffedRiceCrackers
    Nov 12 at 7:26










  • Can you verify type(session['sid']) is a str, just in case it's not another type that just stringifies to admin?
    – Amadan
    Nov 12 at 7:37
















what error you are getting?
– Harsha B
Nov 12 at 6:18




what error you are getting?
– Harsha B
Nov 12 at 6:18












why not just debug the HTML? Inserting {{ session['sid'] }} will display its value and you can then determine whether it is or is not equal to 'admin'
– Attack68
Nov 12 at 7:00




why not just debug the HTML? Inserting {{ session['sid'] }} will display its value and you can then determine whether it is or is not equal to 'admin'
– Attack68
Nov 12 at 7:00












@Attack68 edited my code.
– PuffedRiceCrackers
Nov 12 at 7:23




@Attack68 edited my code.
– PuffedRiceCrackers
Nov 12 at 7:23












@HarshaB There's no error. Just the if statement doesn't work.
– PuffedRiceCrackers
Nov 12 at 7:26




@HarshaB There's no error. Just the if statement doesn't work.
– PuffedRiceCrackers
Nov 12 at 7:26












Can you verify type(session['sid']) is a str, just in case it's not another type that just stringifies to admin?
– Amadan
Nov 12 at 7:37




Can you verify type(session['sid']) is a str, just in case it's not another type that just stringifies to admin?
– Amadan
Nov 12 at 7:37












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










i tested your template (named as template.j2) with jinja-cli and this data file (named as data.json):



{
"session": {
"sid": "admin"
}
}


command line:



# pip install jinja-cli
# jinja -d data.json template.j2


the result is correct:



<ul>

</ul>


so i think the template is fine; you may want to dump session in python code to see if flask really sets its properly; flask.session is a proxy; under the hood it is a signed cookie (doc); so you probably should also check your cookie settings;






share|improve this answer























  • Thanks! I added a header to show what is in session['sid'] as above, and this is getting more frustrating. At least it seems to mean flask sets it properly.
    – PuffedRiceCrackers
    Nov 12 at 7:24












  • @PuffedRiceCrackers i'd recommend upload the whole project in that case so that we can see if anything wrong with its configuration;
    – Cyker
    Nov 12 at 22:30










  • In fact, session['sid'] had whitespaces, something like admin .
    – PuffedRiceCrackers
    Nov 13 at 2:01











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256557%2fif-statement-in-html-with-jinja2-where-should-i-edit-to-make-it-work%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










i tested your template (named as template.j2) with jinja-cli and this data file (named as data.json):



{
"session": {
"sid": "admin"
}
}


command line:



# pip install jinja-cli
# jinja -d data.json template.j2


the result is correct:



<ul>

</ul>


so i think the template is fine; you may want to dump session in python code to see if flask really sets its properly; flask.session is a proxy; under the hood it is a signed cookie (doc); so you probably should also check your cookie settings;






share|improve this answer























  • Thanks! I added a header to show what is in session['sid'] as above, and this is getting more frustrating. At least it seems to mean flask sets it properly.
    – PuffedRiceCrackers
    Nov 12 at 7:24












  • @PuffedRiceCrackers i'd recommend upload the whole project in that case so that we can see if anything wrong with its configuration;
    – Cyker
    Nov 12 at 22:30










  • In fact, session['sid'] had whitespaces, something like admin .
    – PuffedRiceCrackers
    Nov 13 at 2:01















up vote
2
down vote



accepted










i tested your template (named as template.j2) with jinja-cli and this data file (named as data.json):



{
"session": {
"sid": "admin"
}
}


command line:



# pip install jinja-cli
# jinja -d data.json template.j2


the result is correct:



<ul>

</ul>


so i think the template is fine; you may want to dump session in python code to see if flask really sets its properly; flask.session is a proxy; under the hood it is a signed cookie (doc); so you probably should also check your cookie settings;






share|improve this answer























  • Thanks! I added a header to show what is in session['sid'] as above, and this is getting more frustrating. At least it seems to mean flask sets it properly.
    – PuffedRiceCrackers
    Nov 12 at 7:24












  • @PuffedRiceCrackers i'd recommend upload the whole project in that case so that we can see if anything wrong with its configuration;
    – Cyker
    Nov 12 at 22:30










  • In fact, session['sid'] had whitespaces, something like admin .
    – PuffedRiceCrackers
    Nov 13 at 2:01













up vote
2
down vote



accepted







up vote
2
down vote



accepted






i tested your template (named as template.j2) with jinja-cli and this data file (named as data.json):



{
"session": {
"sid": "admin"
}
}


command line:



# pip install jinja-cli
# jinja -d data.json template.j2


the result is correct:



<ul>

</ul>


so i think the template is fine; you may want to dump session in python code to see if flask really sets its properly; flask.session is a proxy; under the hood it is a signed cookie (doc); so you probably should also check your cookie settings;






share|improve this answer














i tested your template (named as template.j2) with jinja-cli and this data file (named as data.json):



{
"session": {
"sid": "admin"
}
}


command line:



# pip install jinja-cli
# jinja -d data.json template.j2


the result is correct:



<ul>

</ul>


so i think the template is fine; you may want to dump session in python code to see if flask really sets its properly; flask.session is a proxy; under the hood it is a signed cookie (doc); so you probably should also check your cookie settings;







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 6:13

























answered Nov 12 at 6:07









Cyker

2,63553245




2,63553245












  • Thanks! I added a header to show what is in session['sid'] as above, and this is getting more frustrating. At least it seems to mean flask sets it properly.
    – PuffedRiceCrackers
    Nov 12 at 7:24












  • @PuffedRiceCrackers i'd recommend upload the whole project in that case so that we can see if anything wrong with its configuration;
    – Cyker
    Nov 12 at 22:30










  • In fact, session['sid'] had whitespaces, something like admin .
    – PuffedRiceCrackers
    Nov 13 at 2:01


















  • Thanks! I added a header to show what is in session['sid'] as above, and this is getting more frustrating. At least it seems to mean flask sets it properly.
    – PuffedRiceCrackers
    Nov 12 at 7:24












  • @PuffedRiceCrackers i'd recommend upload the whole project in that case so that we can see if anything wrong with its configuration;
    – Cyker
    Nov 12 at 22:30










  • In fact, session['sid'] had whitespaces, something like admin .
    – PuffedRiceCrackers
    Nov 13 at 2:01
















Thanks! I added a header to show what is in session['sid'] as above, and this is getting more frustrating. At least it seems to mean flask sets it properly.
– PuffedRiceCrackers
Nov 12 at 7:24






Thanks! I added a header to show what is in session['sid'] as above, and this is getting more frustrating. At least it seems to mean flask sets it properly.
– PuffedRiceCrackers
Nov 12 at 7:24














@PuffedRiceCrackers i'd recommend upload the whole project in that case so that we can see if anything wrong with its configuration;
– Cyker
Nov 12 at 22:30




@PuffedRiceCrackers i'd recommend upload the whole project in that case so that we can see if anything wrong with its configuration;
– Cyker
Nov 12 at 22:30












In fact, session['sid'] had whitespaces, something like admin .
– PuffedRiceCrackers
Nov 13 at 2:01




In fact, session['sid'] had whitespaces, something like admin .
– PuffedRiceCrackers
Nov 13 at 2:01


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256557%2fif-statement-in-html-with-jinja2-where-should-i-edit-to-make-it-work%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine