0

I'm a student studying Django. I want to calculate the number of days between the two specified dates. Currently, View has specified two variables and attempted a subtraction operation, but the following error is being output:

Could not parse the remainder: ' - today' from 'product.due_date|date:"Ymd" - today'

How should I solve this problem? I would appreciate it if you could give me an answer. Attached is models.py and View.py below.

views.py

def product_detail(request, id, product_slug=None):
    current_category = None
    categories = Category.objects.all()
    products = Product.objects.all()
    product = get_object_or_404(Product, product_code=id, slug=product_slug)
    designated_object = Designated.objects.filter(rep_price='True')
    element_object = Element.objects.all()
    value_object = Value.objects.all()
    today = DateFormat(datetime.now()).format('Ymd')

    return render(request, 'zeronine/detail.html', {'product':product,
                                                    'products':products,
                                                    'current_category': current_category,
                                                    'categories':categories,
                                                    'designated_object': designated_object,
                                                    'element_object':element_object,
                                                    'value_object':value_object, 'today':today})

models.py

class Product(models.Model):
    product_code = models.AutoField(primary_key=True)
    username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
    category_code = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, related_name='products')
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True, unique=False, allow_unicode=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    benefit = models.TextField()
    detail = models.TextField()
    target_price = models.IntegerField()
    start_date = models.DateField()
    due_date = models.DateField()

Created as {{product.due_date|date:"Ymd" - today}}.

Jimmar
  • 4,194
  • 2
  • 28
  • 43
gpdnjs12
  • 1
  • 1
  • when you run `format` on the date object you convert it into a string, send the date as is and format it in the template – Jimmar Jul 12 '21 at 14:50
  • Does this answer your question? [Calculate number of days between two dates inside Django templates](https://stackoverflow.com/questions/7887897/calculate-number-of-days-between-two-dates-inside-django-templates) – Jimmar Jul 12 '21 at 14:53
  • 2
    I would advise to define this in the *model*, not in a template: templates implement *rendering logic*, not *business logic*. – Willem Van Onsem Jul 12 '21 at 14:59

0 Answers0