amr_aly

Hakim_date

Jul 11th, 2021 (edited)
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. from django.db import models
  2. from django.db.models.deletion import CASCADE, PROTECT
  3. from django.utils.translation import ugettext_lazy as _
  4. from django.utils import timezone
  5. class Conso(models.Model):
  6.     utlisateur = models.CharField(max_length=100, blank=True, null=True)
  7.     coun_mat = models.IntegerField(blank=True)
  8.     date = models.DateTimeField(blank=True)
  9.     OBS = models.CharField(max_length=200, blank=True, null=True)
  10.     class Meta:
  11.         db_table= "Conso"
  12.         ordering = ['-date']
  13.        
  14.        
  15.        
  16. ###################  views.py
  17. from datetime import datetime, date
  18.  
  19. def index(request, year, month): # to get month you should do this
  20.     today = date.today()
  21.     mat = Conso.objects.filter(date__year=year, date__month=month)
  22.     mat = Conso.objects.filter(date__year=today.year, date__month=today.month)
  23.    
  24.     # this is an example for explanation
  25.     income = Visits.objects.values('visitdate').filter(visitdate__year=2020, visitdate__month=12)
  26.     print(income, income[0], income[0]['visitdate'], income[0]['visitdate'].month)
  27.     # <QuerySet [{'visitdate': datetime.date(2020, 12, 29)}]> {'visitdate': datetime.date(2020, 12, 29)} 2020-12-29 12
  28.    
  29.     ### mat = Conso.objects.all()
  30.     context = {
  31.         'form':mat,
  32.         'month': income[0]['visitdate'].month,
  33.     }
  34.     return render (request, 'index.html', context)
  35.    
  36.  
  37. templates
  38.  
  39. {% for i in form %}
  40. {% if  == %}
  41. ملاحظة اريد القيام بشرط بالشهر
  42.  
  43. {{i.utlisateur}}
  44. {{i.coun_mat}}
  45.  
  46.  
  47. {% endif%}
  48. {% endfor%}
Add Comment
Please, Sign In to add comment