I want to get data from database and send that to html page using django-python.
What I'm doing in python file is
def module1(request):
table_list=student.objects.all()
context={'table_list' : table_list}
return render(request,'index.html',context)
And in html is
<div class="rightbox">
In right box. data is :<br> <br>
{% if table_list %}
<ul> {% for item in table_list %}
<li>{{ item.name }}</li>
<li>{{ item.address }}</li>
<li>{{ item.mob_no }}</li>
{% endfor %}
</ul>
{% else %}
<p>somethings is wrong</p>
{% endif %}
</div>
Nothing is being sent to html file. It is constantly going in else block.
I don't know where I'm making mistake.Please help me.
So far as we don't see your student model (which should be in your models.py and be imported in your vievs.py) and your code doesn't throw any exceptions it seems that your table_list is empty. To iterate it in more convenient way you can use built in for ... empty template tag:
<div class="rightbox">
In right box. data is :<br> <br>
<ul>
{% for item in table_list %}
<li>{{ item.name }}</li>
<li>{{ item.address }}</li>
<li>{{ item.mob_no }}</li>
{% empty %}
<p>somethings is wrong</p>
{% endfor %}
</ul>
</div>
Try this and see what happens. If you'll find yourself in {% empty %} block - your table_list is empty which is points to empty table in database.
Also check docs for this tag.
Related
So I am requesting spot price from cex API, it returns something like this
{"data":[{"amount: "67000.0", "base": "BTC", "currency": "USD"}]} of course there is more than one returned so I want to loop over it.
In my views.py I am passing it into my context 'price': price. Then inside of my .html file I have a list with a for loop for example:
<ul>
{% for x in price %}
<li>{{ x }}</li>
{% endfor %}
</ul>
Then when I open my .html page I get data But what I'd like to be able to do is make a table where I have three columns to show the amount, base and currency. I am unsure on how to extract this data individualy?
You can enumerate over the .values of price, and then enumerate over the dictionaries in that list with:
<table>
{% for vs in price.values %}
{% for v in vs %}
<tr><td>{{ v.amount }}</td><td>{{ v.base }}</td><td>{{ v.currency }}</td></tr>
{% endfor %}
{% endfor %}
</table>
In Django templates, you can access to dictionary items with a expresión like {{ dictionary.key }}
<ul>
{% for x in price %}
<li>{{ x.amount }}</li>
{% endfor %}
</ul>
See the docs on https://docs.djangoproject.com/en/3.2/ref/templates/api/#variables-and-lookups
I want to add a new URL link for each iteration in this for loop:
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
This is my attempt, which I know is wrong because it is basically giving each entry the same CSS link:
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
Maybe try to recreate list entries into list of tuples [(entry, url),] and use it like this:
<ul>
{% for entry, url in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
or just return a separate list of URLs:
<ul>
{% for entry, url in zip(entries, urls) %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
I have a list of objects that I would like to display in an ul/li. For that I do the code below:
<ul id="myUL">
{% for l in lpps %}
<li id="lpps">{{ l.codeACL }} {{ l.libelle }}</li>
{% endfor %}
</ul>
The problem is that in my view, I ask to display only 15 objects per page.
But I want to ignore this and display all the objects on all the pages.
Is there something like for l in lpps.page(all)...?
django have forloop.counter ,
you can use that like
<ul id="myUL">
{% for l in lpps %}
{% if forloop.counter == 15 %}{% break %}{% endif %}
<li id="lpps">{{ l.codeACL }} {{ l.libelle }}</li>
{% endfor %}
</ul>
I am trying to get data from an calendar URL using flask. I have an URL which I am requesting data from. I am getting the output in a list which is not what I want. I want the data from the URL to be rendered in a text-form (like https://www.w3schools.com/html/html_forms.asp) so I can edit the results I get. Right now I can't edit it since its in a list, like this word.
I want it to be something like this "Key : value (the value is in a text-form)".
My current code iterates through the data and renders it in a list instead of a form.
import requests
from flask import Flask
from flask import render_template
app = Flask('__name__')
def get_json(id):
url = 'https://cloud.timeedit.net/ltu/web/schedule1/{0}.json'.format(id)
r = requests.get(url)
return r.json()['reservations']
#app.route('/')
def index():
reservations = get_json('ri107357055Z76Q506656Q65yZ075W2313Y63Q5Q')
return render_template('reservations.html', reservations=reservations)
if __name__ == '__main__':
app.run()
HTML:
e {% for reservation in reservations %}
<ul>
{% for key in reservation %}
{% if key == 'columns' %}
<li>{{ key }} -</li>
<ul>
{% for item in reservation[key] %}
{% if item %}
<li>{{ item }}</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<li>{{ key }} - {{ reservation[key] }} </li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
<hr>
see if this works for you,
Only need to make changes in the html file
{% for reservation in reservations %}
<ul>
{% for key in reservation %}
{% if key == 'columns' %}
<li>{{ key }} -</li>
<ul>
{% for item in reservation[key] %}
{% if item %}
<li>{{ item }}</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<form action="/action_page.php">
{{ key }} - s <input type="text" name="lname", value="{{ reservation[key] }}"><br>
</form>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
<hr>
Output
I have the following code:
<ul>
{% for item in array %}
<li>{{ item }}</li>
{% endfor %}
</ul>
I want to translate the item variable, I've tried using the trans tag like this:
<ul>
{% for item in array %}
<li>{% trans item %}</li>
{% endfor %}
</ul>
But Django complains with a syntax error stating that it was expecting an empty or an endfor
You need to add {% load i18n %} at the top of your template to use the trans tag.
From the docs on internationalization:
To give your template access to these tags, put {% load i18n %}
toward the top of your template.