skip to Main Content

For the purposes of an introductory course in python, Django and Postgresql, I am looking for the best way to retrieve table data and display it in a structured way in an html file with Django and python as a tool and language.

2

Answers


  1. To show the data from Postgres in Django as HTML, you can a lot of ways depending on how you want to show the table. But, this library is a good way to start:
    https://django-tables2.readthedocs.io/en/latest/

    It will do most of the work itself and all you need to do is just pass the qeuryset to the components and it will help you render an HTML table based on your input.

    Login or Signup to reply.
  2. To display django data in html file, simply you can do this:

    In views:

    def login_view(request):
       user = User.objects.all() #Here I have retrieved data from user table, in django table means Model.
       return render(request, 'login.html', {'user':user}) #Here user is context
    

    In html file:

    To display data in html file. here I used curly braces to recognise context in html file

    <html>
    <body>
         {% for data in user %} # user is context from login view, it is used display data in html file.
              {{data}}
         {% endfor %}
    </body>
    </html>
    

    Why I used curly braces in html file because it is template syntax in django

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search