if...else statement in Tornado - if-statement

I have two variables 'usrpermnet' and 'usrpermdev' that can have length of 0 or not. I want compare those length. I wrote this in tornado
if len(usrpermdev) > 0
usrperm = usrpermdev
else
usrperm = usrpermnet
but I have an error.
Which is the way in that I have to write? Thank you.

Try using this:-
{% if len(usrpermdev) > 0 %}
usrperm = usrpermdev
{% else %}
usrperm = usrpermnet
{% end %}

You're missing the colons:
if len(usrpermdev) > 0:
usrperm = usrpermdev
else:
usrperm = usrpermnet
This has nothing to do with Tornado; it's basic Python. You might want to have a look at the Python Tutorial.

Related

How to insert an element into list at index in jinja2

i have set the list of split string in a variable as:
test = a new version of app
{% set new_index = test.split(' ') %}
I am trying to insert 'easy' after 'new' so tried as:
{% set new_index = test.split(' ').insert(2,' easy') %}
then {{ new_index }} which returned None
also tried with a new variable as:
{% set test1 = new_index.insert(2,' easy') %}
this also returned the same None
I read docs in which insert method is never used in examples too
Is there a way to achieve this, any help is appreciated TIA
test = "a new version of app"
new_index = test.split(' ').insert(2,'easy')
print(new_index)
output
None
try This
test = "a new version of app"
new_index = test.split(' ')
new_index.insert(2,'easy')
print(new_index)
output
['a', 'new', 'easy', 'version', 'of', 'app']
Then Try this code for your jinja2 code
{% set new_index = test.split(' ') %}
{% set another_new_index = new_index.insert(2,' easy') %}
then {{ new_index }} would return the required output

Conditional text in variable in ansible

H I want to build a warning message with ansible that is stored in a variable:
I have {{total}} and {{wanted}} and I want to store that in a {{outcome}} variable :
I need something like :
if ({{total}} < {{wanted}}): {{outcome}}= "you need to much"
else if ({{total}} = {{wanted}}): {{outcome}}= "Could work out"
else: {{outcome}}= "Ok"
How can I get this to work?
Thanks for any ideas... this is driving me crazy....
The task below does the job
- set_fact:
outcome: |
{% if total|int < wanted|int %}
You need to much
{% elif total|int == wanted|int %}
Could work out
{% else %}
Ok
{% endif %}

Create loop like codeigniter in django

Controller :
$data['all'] = SQL::get_data($this,tbl_menu,'*',array('parent_id'=>0),'urutan','',null)->result();
foreach ($data['all'] as $all){
$data['child'][$all->id] = SQL::get_data($this,tbl_menu,'*',array('parent_id'=>$all->id),'urutan','',null)->result();
}
in View :
<? $i = 1; foreach ($all as $a) {
$i++; foreach($child[$a->id] as $c ){
}}?>
Working in view.py
for menu in headerData['resData']:
headerData['child'] = {}
headerData['child'][menu.id_menu] = SQL.objects.filter(parent_id=menu.id_menu)
in jinja HTML :
{% for res in resData %}
{{res.id_menu}} #is showing
{% for sub in child.id_menu %}
{{sub.nama}} #not howing
{% endfor %}
{% endfor %}
I want code in codeigniter like this
How To Apply in Django ?
for in query object
for a in all:
child[a]=<your sql command based on database>
You can try this
data = {}
data['all'] = MenuModel.objects.filter(parent=None)
for menu in data['all']:
data['child'][menu.id] = MenuModel.objects.filter(parent_id=menu.id)
If you mean foreach equivalent in python, then you could use :
for variable in iterable
For example you want to access item from an array then use it as index, it would be something like :
for a in all:
print(child[a])

Django templates - comparing variables to integer constants

Seems like elementary question, and yet can't get it work
{% if iterator.next > 10 %}
Do smth
{% endif %}
Two issues. First, this code just won't work (the code in the if-condition never implemented even when the condition seems to hold true), and second issue - the ">" sign is highlighted as if it where the closing tag of the closest open tag. Any ideas how to fix the first issue and is it all right with second issues ? Maybe there's some elegant syntax that I am missing and that would remove this ambiguity for the text editor ?
iterator.next may be a string which would result in the statement being False.
Try creating a custom filter to convert it to an int. For example create "my_filters.py":
# templatetags/my_filters.py
from django import template
register = template.Library()
#register.filter()
def to_int(value):
return int(value)
Then in your template:
{% load my_filters %}
{% if iterator.next|to_int > 10 %}
Do smth
{% endif %}
More on custom tags and filters here
I wouldn't worry about the highlighting, this may just be your IDE. I recommend using PyCharm for Django development
Django's docs says that you can use > with if tag:
{% if somevar < 100 %}
This appears if variable somevar is less than 100.
{% endif %}
take a look at documentation: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/
maybe you are missing something else?

Why is my django view returning a zero obejct list?

I might be lazy or am blind! but the following code is returning ZERO!
items, while very well i know there is some items in the list,
View:
def post_answer(request, quest_id=None):
answer_list = Answer.objects.filter
(questionis__pk=quest_id).select_related()
...
# if a put a print stmt here ( answer_list[0].answer ) there is
# data displayed on the console!!
return render_to_response('myapp/post_answer.html',{'answerobj':answer_list }} )
Template post_answer.html:
{% regroup answerobj.object_list by answer as ans_list %}{{ ans_list|length }}
....
the above code prints 0.
I must be goofing somewhere, pls assist
Paul
try :
{% regroup answerobj by answer as ans_list %}{{ ans_list|length }}
object_list isn't required.