Django Html postgresql | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Django Html postgresql

I have a table in a database, which I handle with postgresql. I fetch all the data and put it into my html with {% for R in Result... <p> {{R.name}} <p>{{R.price}} ... Now I would like to have three columns like: R1.name. R2.name. R3.name R1.price. R2.price. R3.price I can think of some workarounds to achieve that, but I'm wondering, what is best practice? No for loop? Fetch the data from the table in a different way? I think there is probrably a very easy way to do that, which I just don't know, but maybe you know :)

12th Jan 2021, 5:53 PM
Fu Foy
Fu Foy - avatar
2 Answers
+ 2
The best way in my opinion would be to collect the names and prices in the server side names = [r.name for r in your_model_name.objects.all()] prices = [r.prices for r in your_model_name.objects.all()] The pass the `names` and `prices` list into the context dictionary, so that you can access it in the template Then in the html file you can do <table> <tr> {% for name in names %} <td>{{ name }}</td> </tr> <tr> {% for price in prices %} <td>{{ price }}</td> </tr> </table> DISCLAIMER: I haven't done Django in a long time now (like I haven't even touched it since like 6-7 months). So there might be a better way I'm forgetting or I just don't know about. Maybe someone else can provide a better answer.
12th Jan 2021, 7:06 PM
XXX
XXX - avatar
+ 1
Thanks for your idea. I think I'll do it similar to that, unless I find an easier way. I want to keep it as dynamic/flexible as possible.
12th Jan 2021, 10:31 PM
Fu Foy
Fu Foy - avatar