How to display an error message with a function?

Viewed 62

I want to create a basic function for payment. I have several cards and when the user click the button, if the card did not receive a payment this month, it should be refilled, but if did receive a payment this month I want to display an error message in the front end. How can I do it?

views.py

def paycheck(request, id):
    card = Card.objects.get(id=id)
    today = format(datetime.date.today() - datetime.timedelta(days=1), '%B %Y')
    if card.last_payment != today:
        if card.type == "City":
            card.amount = 500
        else:
            card.amount = 300
        card.last_payment = today
        card.save()
    else:
        print("Some error message for client")
    return redirect(request.META['HTTP_REFERER'])

def card(request, id):
    card = Card.objects.get(id=id)
    restaurants = Restaurants.objects.filter(is_member=True)
    logs = CardLogs.objects.filter(card_id=id)
    context = {
        'card': card,
        'restaurants': restaurants,
        'logs': logs
    }
    return render(request, "card.html", context)

card.html

...
<a  class="btn btn-success" href="{% url 'paycheck' card.id %}" >
    Make Payment
</a>
...
0 Answers
Related