populate google chart datatable with arrayToDataTable in django template - python

I have a list of tuples passed to the template.
I want to populate a datatable with it.
Here is the code.
Does anyone know what is wrong with it? Currently nothing shows on the webpage.
Thank you.
var dt=new google.visualization.arrayToDataTable([
['Name','LastGPA','CurGPA','IntervalA','IntervalB','Major'],
{% for d in data %}
[{{d.0}},{{d.1}},{{d.2}},{{d.3}},{{d.4}},{{d.5}}]
{% if not forloop.last %},{% endif %}
{% endfor %}
]);
summary_table.draw(dt);

Here's a helper module for using the Visualization API with Django queries. You might find it easier to work with passing JSON, rather than building it in the template.

Related

How to append specific html to a Django code block using only Python/Django?

I have a form I'm working with in Django.
I have a built in error message I'm trying to get to render on the form.
My first step is to get the error message to render on the form and then I will go into the function and tweak when it shows up.
My problem emerges when it comes to doing it in python.
Normally, my preferred way would be to JQuery for the footer and use JavaScript to append/prepend the HTML. Then set it to show/hide based on conditionals.
However, for this I am wanting to do it in Python to make it easier for the people working w/ me.
Here's an example of the error message HTML I would like to use for appending to something else in JS.
error_field.append('<em for="name" class="form-error-message text-danger">');
Here is an example of the Django Code Block I would like to add it within
{% block form-footer %}
{{ block.super }}
{% endblock %}
What is the easiest way to accomplish this within Python/Django? To be clear, I can figure out the conditional stuff myself. Just the rendering of the specific HTML/CSS error class I have already created. I should be able to do the conditional/function part of this myself.
I can just show you an example, this is a part of my project
views.py
try:
user=User.objects.get(username=username)
except:
messages.info(request,'username does not exist')
return redirect('login')
return render(request,'users/login-register.html')
html
{% if messages %}
{% for i in messages %}
<div class="alert alert--{{i.tags}}">
<p class="alert__message">{{i}}</p>
<button class="alert__close">x</button>
</div>
{% endfor %}
{% endif %}
You can use this anywhere in your html page. This is a common page, and everything in here is extended. And of course this is an example similar to your problem. Check it out if you want

Issues linking data in Flask / MongoDB

New to the whole coding thing, currently doing a course in Python/Flask and giving it my best shot.
I have a MongoDB Atlas DB, with 2 collections in it for drinks.
The structure looks like the below. The values in the array are the _id values of the ingredients in another collection.
_id:5e0f54d31c9d44000015483f
drinkName:"Margarita"
Ingredients:["5e13274e1c9d440000490ed8","5e1327571c9d440000490ed9","5e1327621c9d440000490eda","5e13276b1c9d440000490edb"]
And another collection with the below
_id:5e1327261c9d440000490ed7
ingredientName:"Slice Lemon"
My problem is I'm trying to render the results of the array in Flask on a page. But I can't seem to look up the object id's in the ingredients table, and display the ingredientName of the object.
I can display the array fine, but can't then go to the ingredients collection to get the name.
This is what I have in the html
{% for ingredient in drink.ingredientList %}
{% for ing in ingredients %}
{% if ing._id == ingredient %}
<li>
{{ ing.ingredientName }}
</li>
{% endif %}
{% endfor %}
{% endfor %}
Any help on this would be greatly appreciated, because I feel like I'm banging my head against the wall at this point.

Show url link only if condition meet using data in model - Django

I am using template to render basic html in django and specify link as below.
<li>Geometry</li>
I want to hide this link based on condition using model information.
Does anyone know how to do this?
You can surround the html in an if statement using the Django template language:
{% if object.something %}
<li>Geometry</li>
{% endif %}
You can use operators, filters or complex expressions if object.something is not a boolean
Django has an if template tag, see:
https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#if
from the docs:
{% if not athlete_list %}
There are no athletes.
{% endif %}

display list value in template page using django

I need to display the value of list in template page using django. I have a variable like
data = [('dic1',),('dic2',)]
I pass the value to the template this way:
return render(request,'success.html',{'details':data})
Then in the template I need to show the value. I have used the for loop in the template like:
{% for a in data %}
{{a}}
{% endfor %}
But it displays as
('dic1',)
('dic2',)
when I need to display only the value like below
dic1
dic2
Canyone help me to find out which mistake I did ?
Thanks for your response. I jus to use like below
{% for a in data %}
{{ a.0 }}
{% endfor %}
Now it's working correctly.

Using variables with a django template

I have a developed a filter which does alot of database queries in my django templete. I have several uses for the same filter with the same values which will provide the same result on the same templete. I wish to save the filter result in a variable within the templete so I can make sure I dont re-run the same queries over again.
I cant send this from my view because I am doing this filter call in a for loop for each object in my model.
an example of what I need can be seen in the url tag :
{% url 'path.to.view' arg arg2 as the_url %}
I need the "as" operation over a filter.
{% with bla=arg|foo %}
{% url 'view' bla %}
{% endwith %}

Categories