How to pull data out from database in Django?

Good day!

I'm a beginner, doing website advertising agencies.
Can't display data about the services from the DB table to the template.

Here's what is available:
models:
class Service(models.Model):
 name = models.CharField('Name of service', max_length=40, blank=False, help_text="enter the name of the service")
 price = models.CharField('Price', max_length=15, blank=False, help_text="enter the price for the service")
 description = models.TextField('Description', blank=False, max_length=1000)


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


html:

{% for serv in object_list %}

 {{ serv.name }}
 {{ serv.description }}
 {{ serv.price }} RUB

{% endfor %}


Displays an empty table:
5a747948f2f3a244550054.png

The data in the table are:
5a7481a5a467b634286941.png

What am I doing wrong?
June 8th 19 at 16:37
3 answers
June 8th 19 at 16:39
Solution
The issue was resolved.
As it turned out, after adding the class ServiceListView, I forgot to change urls.py the line ServiceListView.as_view()

Now everything displays as it should. Thanks for all the advice and help!
5a7648b631e55270187841.png
June 8th 19 at 16:41
price better to do decimal or integer, flexibility will be more blank=False is optional.
try changing the view on
def ServiceListView(request):
 serv_list= Service.objects.all().order_by('name')
 returnt render (request, 'services.html', {'serv_list':serv_list})

in the template:
{% for serv in serv_list %}{{ serv.name }}{% endfor %}
Thank you very much for the good advice, will apply. Problem found and the issue resolved. Write the answer in a separate comment. - nikita.Stracke commented on June 8th 19 at 16:44
June 8th 19 at 16:43
change
{{ serv.name }}
on
{{ name }}
Alas, no change - nikita.Stracke commented on June 8th 19 at 16:46
and the data in the table that is ? - nikita.Stracke commented on June 8th 19 at 16:49
Yes.
5a747c7fc4290360076541.png - gage_Osinski commented on June 8th 19 at 16:52
{% for publisher in object_list %}
{{ publisher.name }}
{% endfor %} - nikita.Stracke commented on June 8th 19 at 16:55
in the object_list contains all that is passed to template
just get what other advice was given, it changes the name object_list for the fact that he gave - gage_Osinski commented on June 8th 19 at 16:58
{% for serv in serv_list %} and this should probably serv_list change in object_list - gage_Osinski commented on June 8th 19 at 17:01
, remembering to object_list, the result has not changed.
Removed the name of content_object_name. - gage_Osinski commented on June 8th 19 at 17:04
Yes I understand, but in principle there is no difference, what are you writing if you use object_list on the link here for name jumping model on the field name.

I can not understand why does not want to display, seems to have written everything correctly. - nikita.Stracke commented on June 8th 19 at 17:07
, people working - nikita.Stracke commented on June 8th 19 at 17:10
so I do as it says, was guided by the documentation.

Is there a way to see whether the data in object_list?
Is there a way of checking the display data from the table? - gage_Osinski commented on June 8th 19 at 17:13
{% for publisher in object_list %}
{{ publisher.name }}
{% endfor %}

so it will display all - nikita.Stracke commented on June 8th 19 at 17:16
strange , but doesn't show anything =\

Don't even know what to think. - gage_Osinski commented on June 8th 19 at 17:19
,
1. python manage.py shell and make sure that Service.objects.all().order_by('name') returns not an empty qs.
2. model is optional if you specify a queryset.
3. Not to forget the {% for %} loop in templates is {% empty %} which is executed if the iterable object is empty. - nikita.Stracke commented on June 8th 19 at 17:22
, I'm new, so I would like to know more about how to make sure that Service.objects.all().order_by('name') returns not an empty queryset? - alexandro_Auer commented on June 8th 19 at 17:25
,
Open django shell in cmd/terminal:
python manage.py shell
Import your model:
from myapp.models import Service
where myapp is the name of your appli
Enter
Service.objects.all().order_by('name')
and make sure that the conclusion will be different from this:
<a queryset []></queryset> - nikita.Stracke commented on June 8th 19 at 17:28
,
Service.objects.all().order_by('name')
<a queryset [<service: Outdoor wide format>, <service: Offset printing>, <service: Printing on the texture>, <service: printing>]></service></service></service></queryset>

Lists. - alexandro_Auer commented on June 8th 19 at 17:31
,
Propose to open the source code of your page and watch the table for the presence of data, if they are in the code - fixes your styles.
Another option to check empty qs directly in the template:

{% for serv in object_list %}

 {{ serv.name }}
 {{ serv.description }}
 {{ serv.price }} RUB

{% empty %}

Empty
Empty
Empty

{% endfor %}

If object_list is empty, then in the three columns should be Empty - nikita.Stracke commented on June 8th 19 at 17:34
turns out that it is empty, but why?
5a748fff25eea971440448.png - alexandro_Auer commented on June 8th 19 at 17:37
Not clear, that's all I can offer based on available data.

View looks exactly as in the question or not? Model? - nikita.Stracke commented on June 8th 19 at 17:40
I had a limit of messages.
I found the cause, everything is spelled correctly, but I forgot to change in urls.py the line that you had to write view.ServiceListView.as_view()

Now everything is displayed!
Thank you very much for the tips I remembered them.
Hard to learn easy in battle, as they say.
While not fully mastered, everything seems so difficult. - alexandro_Auer commented on June 8th 19 at 17:43

Find more questions by tags HTMLDjangoPython