A
A
altertable2020-05-12 20:04:48
Django
altertable, 2020-05-12 20:04:48

How to fix Django 3 costing?

Everything is working ! only costile :) how to fix

part of the markup is repeated 7 times since we have tabs (Mon, Tue, Wed ....),
and each tab should display movie sessions on the date that corresponds to the day of the week.
and for this in the view, I pass 7 variables which contains data from the database filtered by the date
indicated below:

tday = date.today() # дата сегодня
        num_week_day = date.today().weekday() # день недели 0..6
       ######################
       #vt = tday + datetime.timedelta(days=1) ## это мы добовляем день(в арг) к текущей дате  
       ######################
        if num_week_day == 0: # если день недели 0 то
            pn = tday + datetime.timedelta(days=0)  # 2020.05.11 <--- сегодня
            vt = tday + datetime.timedelta(days=1)   # 2020.05.12
            sr = tday + datetime.timedelta(days=2)   # 2020.05.13
            ct = tday + datetime.timedelta(days=3)   # 2020.05.14
            pt = tday + datetime.timedelta(days=4)   # 2020.05.15
            sb = tday + datetime.timedelta(days=5)   # 2020.05.16
            nd = tday + datetime.timedelta(days=6)   # 2020.05.17
        if num_week_day == 1:
            pn = tday + datetime.timedelta(days=6) # 2020.05.18
            vt = tday + datetime.timedelta(days=0)  # 2020.05.12 <--- сегодня
            sr = tday + datetime.timedelta(days=1)  # 2020.05.13
            ct = tday + datetime.timedelta(days=2)  # 2020.05.14
            pt = tday + datetime.timedelta(days=3)  # 2020.05.15
            sb = tday + datetime.timedelta(days=4)  # 2020.05.16
            nd = tday + datetime.timedelta(days=5)  # 2020.05.17
        if num_week_day == 2: ... 
        if num_week_day == 3: ... 
        if num_week_day == 4: ...
        if num_week_day == 5: ... 
        if num_week_day == 6: ...


sessions_pn = Session.objects.filter(session_date = pn ).all()
        sessions_vt = Session.objects.filter(session_date = vt ).all()
        sessions_sr = Session.objects.filter(session_date = sr ).all()
        sessions_ct = Session.objects.filter(session_date = ct ).all()
        sessions_pt = Session.objects.filter(session_date = pt ).all()
        sessions_sb = Session.objects.filter(session_date = sb ).all()
        sessions_nd = Session.objects.filter(session_date = nd ).all()


context = {
            
            'sessions_pn': sessions_pn,
            'sessions_vt': sessions_vt,
            'sessions_sr': sessions_sr,
            'sessions_ct': sessions_ct,
            'sessions_pt': sessions_pt,
            'sessions_sb': sessions_sb,
            'sessions_nd': sessions_nd,

            'sessions_time': sessions_time,
            'num_week_day': num_week_day,
            'movies': movies,
            'halls': halls
        }

================================================= =====================================
<div class="sessions">
        <div class="container">
            <h3 class="sessions-top mt-5">
                Розклад фільмів в кінотеатрі СINEMAX 
            </h3>
            <div class="sessions-tabs d-flex align-items-center">
                <nav class="days d-flex">
                    <a href="#pn" class="day tabs-item {% if num_week_day == 0 %} active {% endif %}">пн</a></li>
                    <a href="#vt" class="day tabs-item {% if num_week_day == 1 %} active {% endif %}">вт</a></li>
                    <a href="#sr" class="day tabs-item {% if num_week_day == 2 %} active {% endif %}">ср</a></li>
                    <a href="#ct" class="day tabs-item {% if num_week_day == 3 %} active {% endif %}">чт</a></li>
                    <a href="#pt" class="day tabs-item {% if num_week_day == 4 %} active {% endif %}">пт</a></li>
                    <a href="#sb" class="day tabs-item {% if num_week_day == 5 %} active {% endif %}">сб</a></li>
                    <a href="#nd" class="day tabs-item {% if num_week_day == 6 %} active {% endif %}">нд</a></li>
                </nav>
                <span class="line"></span> 
                
            </div>
            
            <div id="pn" class="sessions-bottom mt-5 tabs-content {% if num_week_day == 0 %} visible {% endif %}">
                
                {% for sess in sessions_pn|slice:":1" %}
                    <p class="mb-5">{{ sess.session_date }}</p>
                {% endfor %} 
                <div class="row text-center">
                    
                    {% if sessions_pn %}
                    
                        {% for movie in movies %}
                            <div class="col-md-2 mb-5">
                                <h6>{{ movie.name }}</h6> <br>

                                {% for session in sessions_pn %}
                                
                                    {% if session.id_film_id == movie.id %}

                                        <a href="{% url 'app:reserve' session.id_hall_id session.id_film_id session.id %}" class="mt-2">
                                            {% for time_s in sessions_time %}
                                                {% if time_s.id == session.id_time_session_id %}
                                                    {{ time_s.time }}
                                                {% endif %}
                                            {%endfor%}
                                            зал № 
                                            {% for i in halls %}
                                                {% if i.id == session.id_hall_id %}
                                                    {{ i.number_hall  }} 
                                                {% endif %}
                                            {% endfor %}
                                        </a> <br> 
                                    {% endif %}   
                                {% endfor %}
                            </div>
                        {% endfor %}
                    {% else %}
                    <div class="alert alert-secondary mx-auto" role="alert">
                        <strong>Сумно !</strong> 
                        в цей день немає сеансів
                    </div>
                    {% endif %}
                </div>
            </div>
            ....
            блок сверху повторяэться 7 раз,только меняться в цыкле {% sessions_pn or sessions_vt ....  %} 
            ....


{% endblock %}


5ebad65cf1fd7096769912.png

5ebad67d2503f873344701.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-05-12
@bacon

Discover data structures - lists, dictionaries, operations on them. As well as all the variety that can be obtained on their basis.
PS I understand correctly that about table links, you also have no idea?

{% for movie in movies %}
    {% for session in sessions_pn %}
        {% if session.id_film_id == movie.id %}

A
alternativshik, 2020-05-12
@alternativshik

*bad shame. Make a query with GROUP BY by date with filter session_date__gte and session_date__lte.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question