0
votes

I want the background color to change every time with Django. For example: bg-success, bg-primary, bg-light, bg-dark... etc. and just randomly. How can I do?

<div class="bg-success">

</div>
1

1 Answers

2
votes

You can just use python builtin module random.choice:

views.py:

def index(request):
    randomclass = random.choice(['bg-success', 'bg-primary', 'bg-light', 'bg-dark'])
    return render(request, 'index.html', {'randomclass': randomclass})

index.html

<div class="{{ randomclass }}">
</div>