Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to retrieve data from MySQL database in Django and display it in html page?

I am trying to add data from MySQL database and display it in a html page using python.

Here is my view.py:

def show(request):
    data = Persons.objects.all()

    person = {
       "Persons": data
    }
    return render(request, "home.html", person)

models.py:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Persons(models.Model):   
   PersonID = models.IntegerField()
   LastName = models.CharField(max_length=100)
   FirstName = models.CharField(max_length=100)
   Address = models.CharField(max_length=100)
   City = models.CharField(max_length=100)
   class Meta:
      db_table = "persons"

html:

  <div>
      {% for item in Person %}
          <div class="numbers">{{ item.LastName }}</div>
       {% endfor %}
  </div> 

But I didnot retrieve anything.
Any help will be highly appreciated.

>Solution :

You create a dict key Persons

person = {
   "Persons": data
}

…but you iterate over Person

{% for item in Person %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading