Show text on page from Django database

Asked

Viewed 7 times

-1

Hello, I’m trying to display a text from the mysql database, using Django, I’ve searched for several tutorials but nothing works, if anyone can help thank you already.

This is part of my file: index.html

<div class="col-lg-4 col-6">
  <div class="small-box bg-warning">
    <div class="inner">
      {% for rf in indrf %}
      <h3>{{rf.id}}</h3>
      {% endfor %}
      <p>Indicações registradas</p>
    </div>

This is my model file: py.models

from django.db import models

class indications(models.Model):
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=15)
    obs = models.TextField()
    status = models.CharField(max_length=10)
    user_id = models.CharField(max_length=50)

This is part of the view: py views.

def countrefused(request):
    indrf = indications.objects.all()
    return render(request,"index.html",{'indrf':indrf})

But every time I run, the code doesn’t return anything. Grateful!!

1 answer

0

def __str__(self):
        return f'{name} {phone} {obs} {status}'

__str__ is a function used to return a string representation of an object in the class indications
( Class name always uppercase to differentiate from objects and functions Indications should be )

applying in the code class Indications

from django.db import models

class indications(models.Model):
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=15)
    obs = models.TextField()
    status = models.CharField(max_length=10)
   # user_id = models.CharField(max_length=50) django seta como auto incremeto o ids
    def __str__(self):
        return f'{name} {phone} {obs} {status}'

this recovering phone Obs name and status. can indicate which will be returned not necessarily can be all.

on the html page will be passed the names as:

{% for rf in indrf %}
      <h3>{{rf.id}} {{rf.name}} {{rf.phone}}</h3>
      {% endfor %}
      <p>Indicações registradas</p>
  • Thanks for your help, it worked properly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.