Dynamic html table with django [closed]

2024/10/10 6:18:53

I want to realize HTML page with table which consist from date from database. And if I add element to database I want to HTML table updated too. How to realize it with django?

Answer

You can write something like this:

In your models.py file :

class MyModel(models.Model):foo = models.CharField(max_length=...)bar = models.CharField(max_length=...)...def __str__(self):return self.foo, self.bar

Then, in your views.py file:

def MyFunction(request):my_var = MyModel.objects.all()return render(request, 'Template.html', {"my_var": my_var})

And finally in your template.html file :

{% load staticfiles %}
{% load static %}{% block content %}{% for object in my_var_list %}<table style="width:90%"><tbody><p></p><tr><td>foo</td><td>{{ object.foo }}</td></tr><tr><td>bar</td><td>{{ object.bar }}</td></tr></tbody>
</table>{% endfor %}{% endblock content %}

Next time, read StackOverflow documentations, and what you have done before to post your question.

https://en.xdnf.cn/q/118485.html

Related Q&A

Driving costs - functions in python- EOFerror in if __name__ == __main__:

I am stuck with this question: Write a function driving_cost() with input parameters miles_per_gallon, dollars_per_gallon, and miles_driven, that returns the dollar cost to drive those miles. All items…

Pandas loc dynamic conditional list

I have a Pandas DataFrame and I want to find all rows where the ith column values are 10 times greater than other columns. Here is an example of my DataFrame:For example, looking at column i=0, row B (…

no module named numpy python2.7

Im using python 2.7 on Linux CentOS 6.5. After successfully using yum to install numpy, I am unable to import the module.from numpy import *The above code produces the following error:no module named …

TypeError: list indices must be integers or slices, not tuple for list of tuples

I am getting "list indices must be integers or slices, not tuple" error while trying to generate list from list of tuples. list of tuples have the following structure:[(29208, 8, 8, 8), (2920…

Spark Unique pair in cartesian product

I have this:In [1]:a = sc.parallelize([a,b,c]) In [2]:a.cartesian(a).collect() Out[3]: [(a, a), (a, b), (a, c), (b, a), (c, a), (b, b), (b, c), (c, b), (c, c)]I want the following result:In [1]:a = sc.…

How to use double click bid manager(DBM) API in python

I am trying to use the google Double click bid manager (DBM) API, to download reports, I am trying to make this automatic without manual authentication, but all I can find is the GitHub repo for DBM sa…

How can I replace a value in an existing excel csv file using a python program?

How can I update a value in an existing .csv file using a python program. At the moment the file is read into the program but I need to be able to change this value using my program, and for the change…

Why might Python break down halfway through a loop? TypeError: __getitem__

The GoalI have a directory with 65 .txt files, which I am parsing, one by one, and saving the outputs into 65 corresponding .txt files. I then plan to concatenate them, but Im not sure if jumping strai…

RoboBrowser getting type error NoneType object is not subscriptable

Im trying to make a kahoot spammer which inputs a pin number and a username, decided by the user. Im getting a type error when I run this code:import re from robobrowser import RoboBrowser#Getting pin …

How to create a 2d list with all same values but can alter multiple elements within? (python)

Im trying to create a list that holds this exact format: [[2],[2],[2],[2],[2],[2],[2],[2],[2],[2]]and when list[3][0] = 9 is called, the list becomes [[2],[9],[2],[9],[2],[9],[2],[9],[2],[9]]How do I c…