0

I am trying to retrieve 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:

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.

1 Answer 1

1

You create a dict key Persons

person = {
   "Persons": data
}

...but you iterate over Person

{% for item in Person %}
Sign up to request clarification or add additional context in comments.

4 Comments

If I am iterating over Persons I am getting error Table 'mysql.persons' doesn't exist"
That's the table you specified in your Model db_table = "persons". Does the table exist?
Yes, the table exists is MySQL database.
To debug, use the Django shell to access the table. ./manage.py shell. Then import your Persons Model and try to read from it. If that still doesn't work, the bug is probably in you database settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.