G
G
gromyko212020-05-23 08:24:50
Django
gromyko21, 2020-05-23 08:24:50

How to properly make a Django product catalog?

Hello. I am writing a site on Django and the question arose of how to make the catalog of goods correctly (if there is a ready-made script, or an example - throw it. I will be grateful)?
This is not an online store, that is, the functions of the basket and purchases are not needed. It is necessary to load data from the admin panel (at first, the new reverse for some reason does not work as I want) and automatically create a new page when 9 products are loaded into the admin panel.
The main question is: how to upload new data first and create new catalog pages when filling out? An example in the photo.5ec8b38a19f44814843732.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Alyukov, 2020-05-23
@mvxmvl

In view file:

from django.shortcuts import render


def index(request):
    name_query = Post.objects.order_by('-pub_date')
    return render(request, 'shop/index.html', context={'data_name_query': name_query}

In the index.html file:
...
{% for some in data_name_query %}
    {{ some.title }}
{% empty %}
    <p>Нет данных для построения</p>
{% endfor %}
...

Explanations:
  1. Be sure to make sure you have render connected in the view file
  2. name_query - you are free to name whatever you like
  3. Post is the name of the class in the model.py file that is being requested
  4. order_by('-pub_date') - sort by "pub_date" field, "-" sorts in reverse order
  5. 'shop/index.html' - path to your html file
  6. context - the data that will be transferred to the html file (I specified the name of the data as data_name_query, you are free to name it as you like)
  7. In the index.html file, we iterate over the received data from context. The variable was named "data_name_query", so we loop through it, pull out an object from it and give it the name "some"
  8. {{ some.title }} is a reference to a specific field that you specified in model.py to a specific class

D
Dr. Bacon, 2020-05-23
@bacon

Are you sure you need Django here, and not some CMS? And then on the question it seems that you do not understand at all what and how.
1. What does reverse have to do with it?
2. What does it mean to upload new ones first? sort queryset or what?
3. New pages are created automatically, you need to write a view for them once

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question