Skip to content

Instantly share code, notes, and snippets.

@umarhussain88
Created March 5, 2021 14:00
Show Gist options
  • Save umarhussain88/14c447de40f07d998d3fa5fd496bfeb2 to your computer and use it in GitHub Desktop.
Save umarhussain88/14c447de40f07d998d3fa5fd496bfeb2 to your computer and use it in GitHub Desktop.
from .models import Carbon
from django.db.models import Q
from django.views.generic import ListView
from datetime import datetime
class CarbonDataView(ListView):
model = Carbon
context_object_name = 'carbon'
template_name = 'carbon/chart.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
query = self.request.GET.get('q')
context['date'] = Carbon.objects.values('reading_date').order_by('reading_date').distinct()
if query:
context['carbon'] = Carbon.objects.filter(
Q(reading_date=datetime.strptime(query,"%Y,%b,%d"))
)
context['selected_date'] = datetime.strptime(query,"%Y,%b,%d")
else:
context['carbon'] = Carbon.objects.filter(
Q(reading_date=datetime(2017,1,1))
)
context['selected_date'] = datetime(2017,1,1)
return context
# class CarbonExportView()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment