I'm stupid
I'm stupid

Reputation: 67

django how to display a weeks data

Hi thanks for helping me

I being doing some browsing on google and stack overflow, but documentation django and python I can hardly understand, how the they make code run

I just can't figure out a way to display week data (table & chart) with two toggle button to toggle different weeks of 53 weeks in a year

I had try using the week in Django template tags; https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#date but I get empty value instead; here example I did {{ value|date:"W" }}

is there easily way to do this? I do not wish to use the weekarchiveview: https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-date-based/#weekarchiveview

As I need to toggle between years,months and weeks on same page.

Below are my codes

this my code for views

from django.shortcuts import render
from django.views.generic import ListView, DetailView ,TemplateView
from zigview.models import tank_system

from django.utils import timezone
from datetime import date, timedelta    
class EC(ListView):
        model = tank_system
        template_name = 'FrounterWeb/extends/EC.html'
        ordering = ['-datetime']  # sort dates in descending order

        def get_context_data(self, **kwargs):
            return {'tank': self.get_queryset()}

This is my apps url codes

from django.urls import path
from . import views #function views
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required, permission_required

urlpatterns = [

    path('',login_required(views.index.as_view()), name='index'), # views to call our index
    path(r'', TemplateView.as_view(template_name='index.html'), name = 'myapp'), # tell django the which path to our main page
    path(r'liveSterm/', login_required(views.Strem), name='Livesterm'), #call live strem page
    path(r'EC-data/', login_required(views.EC.as_view()), name='EC'),
    path(r'ph-data/', login_required(views.ph.as_view()), name='ph'),
    path(r'Water-Temptures/', login_required(views.WT.as_view()), name='WT'),
    path(r'Room-Temptures/', login_required(views.RT.as_view()), name= 'RT'),
    path(r'Water-Flow-IN/', login_required(views.WaterFlowIN.as_view()), name= 'WFI'),
    path(r'Water-Flow-OUT/', login_required(views.WaterFlowOUT.as_view()), name= 'WFO'),
    ]

this is my models codes

from django.db import models
from django.utils import timezone
from decimal import Decimal
# having errors KeyError: "'__name__' not in globals"

class tank_system(models.Model):

    PH = models.DecimalField(max_digits=3, decimal_places=1)
    EC = models.DecimalField(max_digits=3, decimal_places=1)
    Winlet = models.DecimalField(max_digits=3, decimal_places=1)
    Woutlet = models.DecimalField(max_digits=3, decimal_places=1)
    WaterLevel = models.IntegerField(default=500)
    TempWater = models.IntegerField(default=25)
    TempRoom = models.IntegerField(default=25)
    tanks = models.IntegerField(default=1)
    datetime = models.DateTimeField(default=timezone.now())

Upvotes: 1

Views: 1638

Answers (1)

Roy Zwambag
Roy Zwambag

Reputation: 76

You don't have 'value' in your context. The context is only 'tank'. So to get the datetime you can use {{ tank.datetime|date:"W" }}

To switch after clicking on a button you can write a simple piece of javascript that changes the inner html of a certain part of your DOM to {{ tank.datetime|date:"W" }} , {{ tank.datetime|date:"M" }} etc. after clicking the button

Upvotes: 1

Related Questions