O
O
ooker2020-10-22 19:31:56
Django
ooker, 2020-10-22 19:31:56

Proper display of multiple models per page in Django?

Good afternoon. I can't figure out how to correctly display the sequence of blocks on the main page.
It is necessary to display the block last added by date, but there are several models. When displayed, the existing ones do not let the top ones. There is an option to make from the current code, or you need to make a new model (one of all).
Here is the html code of the page

{% for haircut in haircut %}
          <div class="blog vertical-blog col-lg-4 col-md-4 col-sm-6 col-xs-12">
                        <div class="blog-foto"><a href="{{ haircut.get_absolute_url }}"><img src="/media/{{ haircut.haircut_image_field }}"></a></div>
            <div class="blog-date">{{ haircut.haircut_datetime }}</div>
            <div class="blog-subtitle"><a href="{% url 'blog_haircut' %}">Парикмахерская</a></div>
            <div class="blog-title"><a href="{{ haircut.get_absolute_url }}">{{ haircut.haircut_title }}</a></div>
                    </div>
                    {% endfor %}
                    {% for nails in nails %}
          <div class="blog vertical-blog col-lg-4 col-md-4 col-sm-6 col-xs-12">
                        <div class="blog-foto"><a href="{{ nails.get_absolute_url }}"><img src="/media/{{ nails.nails_image_field }}"></a></div>
            <div class="blog-date">{{ nails.nails_datetime }}</div>
            <div class="blog-subtitle"><a href="{% url 'blog_nails' %}">Маникюр</a></div>
            <div class="blog-title"><a href="{{ nails.get_absolute_url }}">{{ nails.nails_title }}</a></div>
                    </div>
                    {% endfor %}


view
class BlogMainPage(TemplateView):  # Главная страница 
    template_name = "blog/blog.html"

    def get(self, request, **kwargs):
        haircut = BlogArticlesHairCut.objects.all()
        nails = BlogArticlesNail.objects.all()
        cosmetologia = BlogArticlesCosmetologia.objects.all()
        tatoo = BlogArticlesTatoo.objects.all()
        spa = BlogArticlesSpa.objects.all()
        makeup = BlogArticlesMakeUp.objects.all()
        depil = BlogArticlesDepil.objects.all()
        promoheader = BlogBlock1.objects.all()
        promocenter = BlogBlock2.objects.all()
        promofooter = BlogBlock3.objects.all()
        ctx = {
            'haircut': haircut,
            'nails': nails,
            'tatoo': tatoo,
            'spa': spa,
            'depil': depil,
            'cosmetologia': cosmetologia,
            'makeup': makeup,
            'promoheader': promoheader,
            'promocenter': promocenter,
            'promofooter': promofooter,
        }
        return render(request, self.template_name, ctx)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2020-10-22
@ooker

You generally have some kind of tin with the architecture, in addition to the errors that were cited above.
Doesn't it bother you at all that you are trying to put so many objects on one page?
Surely you can select the BlogArticles model in which to make common fields. For example, the date of adding - you already have a common one for all blocks. And display on the page BlogArticles
And to it already sculpt specific models - the same OneToOne relations.
And there your question will be solved by itself ...

D
Dr. Bacon, 2020-10-22
@bacon

1. TemplateView needs to override get_context_data, not get
2. why this bunch of BlogArticlesXXX, I suspect that one BlogArticles model is enough, with promo it’s the same
3. it’s customary to give the model name in the singular, and in the fields do not duplicate the name not nails_title, but just title
4.

{% for haircut in haircut %}, {% for nails in nails %}
this fox needs to be explained?

V
Vladimir, 2020-10-24
@AstraVlad

Did I understand correctly that these are all blog entries? Then why make different models for records of different topics? We make one single model and put down the "Subject" field in it (hairstyles, nails, etc.). Before output, sort by topic and display in one cycle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question