Search code examples
pythondjangodjango-templatesorm

Django not displayed data from db


db image result

#models.py
class Text(models.Model):
    id = models.CharField(max_length=255, primary_key=True, blank=False)
    text = models.TextField(blank=False)enter image description here
    public = models.BooleanField()


#views.py
def home(request):
    data = Text.objects.all()
    data = {
    'data': data
    }
    return render(request, 'app1/index.html', data)


#index.html
  <div class="container p-4 g-col-6 w-50  ">
      {% if data %}
        {% for i in data %}
        <div class="row bg-dark text-white  mt-3 rounded">
          <p class="p-1 text-info">{{ i.text }}</p>
        </div>
        {% endfor %}
      {% else %}
        <p>not data</p>
      {% endif %}enter image description hereenter image description here
  </div>'

I tried to output the manually recorded data several times - it was displayed perfectly, but when it comes to data from the model, nothing is displayed

I got:

    <QuerySet []>
    not data

when added data before if/else

<div class="container p-4 g-col-6 w-50  ">
    {{ data }}
      {% if data %}
        {% for i in data %}
        <div class="row bg-dark text-white  mt-3 rounded">
          <p class="p-1 text-info">{{ i.text }}</p>
        </div>
        {% endfor %}
      {% else %}
        <p>not data</p>
      {% endif %}
 </div>

Solved In Datagrip, I have got two connections, hence two different tables with names 'Text' - done from alchemy, and 'app1_text' - empty. I solved this just rename table name from alchemy and added data to django table.


Solution

  • Your db is wrong.

    Your Text table is not used in Django. Its just useless. There is app1_text which Django specially created for this model and it is empty.

    Move all data from Text to app1_text but make sure thats everything fits Django model.

    p.s.: all your custom django models will look in database like AppName_ModelName