Create loop like codeigniter in django - 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])

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 %}

How to parse the following dict in django templates?

I have a following dict structure passed to my Django template, whose keys or length are unknown to me:
config_values = {
'Kafka': {
'KAFKA_BROKER_URL': 'localhost:9092',
},
'Redis': {
'REDIS_HOST': 'localhost',
'REDIS_PORT': '6379',
},
}
I want to show it in my templates as shown below
Kafka
KAFKA_BROKER_URL = localhost:9092
Redis
REDIS_HOST = localhost
REDIS_PORT = 6379
I'm relatively new to python and dicts.
You can iterate through a dict in a django template just the way you do in python.
Look at this.
{% for i,j in config_file.items %}
{{i}} // i will give you 'kafka'
{% for k,l in j.items %}
{{k}} {{l}} // k will give you 'kafka_broker_url' and l will give you localhost:9092
{% endfor %}
{% endfor %}

yesno filter for variable assignment inside with tag inside blocktrans

I'm trying to set a variable as follow :
{% blocktrans with car_type=car.get_car_type_display article=(car.get_car_type_display=="XL"|yesno:'an,a') %}
but it doesn't work:
Could not parse some characters: |(car.get_car_type_display=="XL"||yesno:'an,a')
Could you please help me through this ?
You can't use the boolean comparison expression like that. You should make that comparison into a pseudo property on the model so you can do this:
{% blocktrans with article=car.car_display_type_is_xl|yesno:'an,a' %}
Where your car model has the new method:
#property
def car_display_type_is_xl(self):
return self.get_cart_type_display() == "XL"

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.