S
S
Storm Trooper2018-02-02 17:45:12
Django
Storm Trooper, 2018-02-02 17:45:12

How to correctly retrieve data from a database in Django?

Good day!
I'm new, I'm making a website for an advertising agency.
I can not display data about services from the database table to the template.
Here is what is available:
models :

class Service(models.Model):
    name = models.CharField('Наименование услуги', max_length=40, blank=False, help_text="Заполните наименование услуги")
    price = models.CharField('Цена', max_length=15, blank=False, help_text="укажите цену за услугу")
    description = models.TextField('Описание услуги', blank=False, max_length=1000)

view :
class ServiceListView(ListView):
    model = Service
    template_name = 'services.html'
    queryset = Service.objects.all().order_by('name')

html :
<tbody>
{% for serv in object_list %}
    <tr>
        <td>{{ serv.name }}</td>
        <td>{{ serv.description }}</td>
        <td>{{ serv.price }} руб.</td>
    </tr>
{% endfor %}
</tbody>

Displays an empty table:
5a747948f2f3a244550054.png
There is data in the table:
5a7481a5a467b634286941.png
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Storm Trooper, 2018-02-04
@n0str0m0

The issue was resolved.
As it turned out, after adding the class ServiceListView, I forgot to change the line in urls.py to ServiceListView.as_view()
Now everything is displayed as it should. Thanks everyone for the advice and help!
5a7648b631e55270187841.png

V
vikholodov, 2018-02-03
@vikholodov

price is better to do decimal or integer, there will be more flexibility, blank=False is optional.
try replacing the view with

def ServiceListView(request):
    serv_list= Service.objects.all().order_by('name')
    returnt render (request, 'services.html', {'serv_list':serv_list})

in template:
{% for serv in serv_list %}<td>{{ serv.name }}</td>{% endfor %}

V
Vladimir, 2018-02-02
@vintello

change to
<td>{{ name }}</td>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question