0

So basically I have a table with 16 objects. I want to show only four of them on my website and want it to automatically rotate every week to another four and so on. I was thinking maybe a cronjob would do it, but I rather ask here before I take any action.

Here is my models.py

class Liturgicketexty(models.Model):
    id = models.AutoField(primary_key=True)
    text = models.CharField(max_length=45)
    url = models.URLField(max_length=200, blank=True, null=True)

    class Meta:
        verbose_name_plural = 'Liturgické texty'

    def __str__(self):
        return self.text

and here is the part in html where I use this table.

<div id="table">
                    <table>
                        <thead>
                            <tr>
                                <th>Čtení na neděli</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for l in liturgicketexty %}
                            <tr>
                                <td><a href="{{ l.url }}">{{ l.text }}</a></td>
                            </tr>
                            {% endfor %}

                        </tbody>
                    </table>
</div>

If someone needs it.

Like I said. I tried to research some solution, but found nothing promising. If you find or know something I will be more than grateful.

StefanoTrv
  • 199
  • 7
H3ry
  • 1

2 Answers2

0

Here is an example of a view that selects 4 different objects each week. After four weeks, it goes back to showing the first 4 objects, and so on.

def exampleView(request):
    # gets a number between 0 and 4, based on the current week
    week_group = int(datetime.now().strftime("%W")) % 4
    # chooses 4 objects using the previous number
    this_weeks_objects = Liturgicketexty.objects.all()[4*week_group:4*(week_group+1)+1]
    # add the objects to the context
    context = {"liturgicketexty": this_weeks_objects }
    # renders your template, which doesn't need to be modified.
    return render(request, "your_template.html", context)

datetime.now().strftime("%W") returns the number of the current week (more details here).

StefanoTrv
  • 199
  • 7
0

It's best to go with a cron job. In my experience any code inside a view is only executed when someone visits that view, and also, it's not a good idea to do such computation inside a view function. For instance, if your website have 1000 visitors per day on that view, that means you will be doing that amount of computation on this view alone, and if you choose to store the current week in the database, you will also have to update the DB per call or visit

But if you go with a cron job, you only update the view once per week.

Bruno
  • 655
  • 8
  • 18
  • So if I go with cronjob how would u do it? I have not yet used cronjob only read about it and in summary I know what it does. Also my project uses MySQL database and thought if event in MySQL could do it. – H3ry Jun 07 '23 at 19:45
  • Just to put things into prospective, 1000 visits a day on that specific page means on average a call to that view every minute and a half. The view involves making a query on a very small table and a few other very simple calculations. Unless the number of visitors is much bigger (or there are big peaks of visitors during the day), conjob would be overkill for the purpose. – StefanoTrv Jun 07 '23 at 21:49