django如何显示一个星期的数据

时间:2018-12-13 07:53:46

标签: django datetime django-views django-queryset django-class-based-views

您好,谢谢您的帮助

我正在Google上进行一些浏览和堆栈溢出,但是我几乎不了解django和python文档,它们如何使代码运行

我只是想不出一种方法来显示周数据(表格和图表) 两个切换按钮可切换一年中53周的不同周

我尝试在Django模板标签中使用“星期”; https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#date 但是我得到的是空值;在此示例中,我做了{{ value|date:"W" }}

有没有简便的方法可以做到这一点?我不希望使用weekarchiveview: https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-date-based/#weekarchiveview

我需要在同一页面上的年,月和周之间进行切换。

下面是我的代码

这是我的视图代码

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()}

这是我的应用的网址代码

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'),
    ]

这是我的模特代码

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())

1 个答案:

答案 0 :(得分:1)

您的上下文中没有“价值”。上下文仅仅是“坦克”。因此,要获取日期时间,您可以使用{{ tank.datetime|date:"W" }}

要在单击按钮后进行切换,您可以编写一段简单的JavaScript,在单击按钮后,将DOM特定部分的内部html更改为{{ tank.datetime|date:"W" }}{{ tank.datetime|date:"M" }}