Skip to content Skip to sidebar Skip to footer

Dynamic Html Table With Django

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?

Solution 1:

You can write something like this:

In your models.py file :

classMyModel(models.Model):

    foo = models.CharField(max_length=...)
    bar = models.CharField(max_length=...)
    ...

    def__str__(self):
        returnself.foo, self.bar

Then, in your views.py file:

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

<tablestyle="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.

Post a Comment for "Dynamic Html Table With Django"