Answer the question
In order to leave comments, you need to log in
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)
class ServiceListView(ListView):
model = Service
template_name = 'services.html'
queryset = Service.objects.all().order_by('name')
<tbody>
{% for serv in object_list %}
<tr>
<td>{{ serv.name }}</td>
<td>{{ serv.description }}</td>
<td>{{ serv.price }} руб.</td>
</tr>
{% endfor %}
</tbody>
Answer the question
In order to leave comments, you need to log in
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!
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})
{% for serv in serv_list %}<td>{{ serv.name }}</td>{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question