在web2py中按日期生成特定数据

时间:2017-05-20 03:14:52

标签: python date web2py

我坚持申请。 我希望按日期从数据库生成特定数据,我想要选择。

这是我的模特:

# -*- coding: utf-8 -*-

# on_define_lion
def on_define_lion(table):
    # represent
    table.time_taken.represent = ads_field_constructor.represent_integer
    table.ukuran_lebar_toko.represent = ads_field_constructor.represent_integer
    table.weeks.represent = ads_field_constructor.represent_integer
    table.mama_lemon_penjualan.represent = ads_field_constructor.represent_integer
    table.zinc_refreashing_cool_penjualan.represent = ads_field_constructor.represent_integer
    table.zinc_black_shine_penjualan.represent = ads_field_constructor.represent_integer
    table.systema_spring_fresh_penjualan.represent = ads_field_constructor.represent_integer
    table.systema_mentol_breeze_penjualan.represent = ads_field_constructor.represent_integer
    table.emeron_anti_dandruff_penjualan.represent = ads_field_constructor.represent_integer
    table.emeron_black_shine_penjualan.represent = ads_field_constructor.represent_integer
    table.ciptadent_maxi_12_penjualan.represent = ads_field_constructor.represent_integer
    table.mama_lemon_stock.represent = ads_field_constructor.represent_integer
    table.zinc_refreashing_cool_stock.represent = ads_field_constructor.represent_integer
    table.zinc_black_shine_stock.represent = ads_field_constructor.represent_integer
    table.systema_spring_fresh_stock.represent = ads_field_constructor.represent_integer
    table.systema_mentol_breeze_stock.represent = ads_field_constructor.represent_integer
    table.emeron_anti_dandruff_stock.represent = ads_field_constructor.represent_integer
    table.emeron_black_shine_stock.represent = ads_field_constructor.represent_integer
    table.ciptadent_maxi_12_stock.represent = ads_field_constructor.represent_integer
    table.total_transaksi_dalam_satuan_rupiah.represent = ads_field_constructor.represent_integer
    table.foto_outlet.represent = ads_field_constructor.represent_url
    table.foto_bukti_trasnsaksi.represent = ads_field_constructor.represent_url
    table.foto_display_produk_1.represent = ads_field_constructor.represent_url
    table.foto_produk_dislay_2.represent = ads_field_constructor.represent_url
    table.foto_posm_1.represent = ads_field_constructor.represent_url
    table.foto_posm_2.represent = ads_field_constructor.represent_url
    # requires
    table.auditor.requires = IS_NOT_EMPTY()
    table.date_of_response.requires = ads_field_constructor.requires_date()
    table.sync_date.requires = ads_field_constructor.requires_date()
    table.time_of_response.requires = IS_TIME()
    table.sync_time.requires = IS_TIME()
    table.jenis_kunjungan.requires = ads_field_constructor.requires_set_not_empty(list_register_outlet)
    table.berada_di_dalam_pasar_atau_tidak.requires = ads_field_constructor.requires_set_not_empty(list_pasar)
    table.type_outlet.requires = ads_field_constructor.requires_set_not_empty(list_type_outlet)
    table.katagori_produk_yang_di_jual_sembako.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_food.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_beverage.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_ice_cream.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_personal_care.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_household.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_cigarette.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_medicine.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_vitamin.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.katagori_produk_yang_di_jual_baby_care.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.kondisi_yang_ada_di_toko_chiller.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.kondisi_yang_ada_di_toko_coolbox.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.kondisi_yang_ada_di_toko_freezer_ice_cream.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.kondisi_yang_ada_di_toko_rak_rokok_special.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.kondisi_yang_ada_di_toko_special_branding.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.kondisi_yang_ada_di_toko_program_display.requires = ads_field_constructor.requires_set_not_empty(list_yes)
    table.di_kunjungi_salesman_principle_terkait_program_ads.requires = ads_field_constructor.requires_set_not_empty(list_ya)
    table.apakah_outlet_melakukan_transaksi.requires = ads_field_constructor.requires_set_not_empty(list_beli)
    table.alasan_tidak_mau_beli.requires = ads_field_constructor.requires_set_not_empty(list_alasan_tidak_beli)

# create table : lion
db.define_table('lion', 
    Field('uuid'), 
    Field('auditor'), 
    Field('date_of_response', 'date'), 
    Field('time_of_response', 'time'), 
    Field('sync_date', 'date'), 
    Field('sync_time', 'time'), 
    Field('geo_location'), 
    Field('time_taken', 'integer'), 
    Field('jenis_kunjungan'), 
    Field('data_toko'), 
    Field('id_outlet'), 
    Field('nama_outlet'), 
    Field('area'), 
    Field('tag_location'), 
    Field('alamat'), 
    Field('kecamatan'), 
    Field('patokan'), 
    Field('berada_di_dalam_pasar_atau_tidak'), 
    Field('type_outlet'), 
    Field('katagori_produk_yang_di_jual_sembako'), 
    Field('katagori_produk_yang_di_jual_food'), 
    Field('katagori_produk_yang_di_jual_beverage'), 
    Field('katagori_produk_yang_di_jual_ice_cream'), 
    Field('katagori_produk_yang_di_jual_personal_care'), 
    Field('katagori_produk_yang_di_jual_household'), 
    Field('katagori_produk_yang_di_jual_cigarette'), 
    Field('katagori_produk_yang_di_jual_medicine'), 
    Field('katagori_produk_yang_di_jual_vitamin'), 
    Field('katagori_produk_yang_di_jual_baby_care'), 
    Field('kondisi_yang_ada_di_toko_chiller'), 
    Field('kondisi_yang_ada_di_toko_coolbox'), 
    Field('kondisi_yang_ada_di_toko_freezer_ice_cream'), 
    Field('kondisi_yang_ada_di_toko_rak_rokok_special'), 
    Field('kondisi_yang_ada_di_toko_special_branding'), 
    Field('kondisi_yang_ada_di_toko_program_display'), 
    Field('ukuran_lebar_toko', 'integer'), 
    Field('di_kunjungi_salesman_principle_terkait_program_ads'), 
    Field('nama_perusahaan_salesman_tersebut'), 
    Field('alasan_besedia_beli_di_motoris_adshouse'), 
    Field('foto_outlet'), 
    Field('data_transaksi'), 
    Field('weeks', 'integer'), 
    Field('apakah_outlet_melakukan_transaksi'), 
    Field('alasan_tidak_mau_beli'), 
    Field('mama_lemon_penjualan', 'integer'), 
    Field('zinc_refreashing_cool_penjualan', 'integer'), 
    Field('zinc_black_shine_penjualan', 'integer'), 
    Field('systema_spring_fresh_penjualan', 'integer'), 
    Field('systema_mentol_breeze_penjualan', 'integer'), 
    Field('emeron_anti_dandruff_penjualan', 'integer'), 
    Field('emeron_black_shine_penjualan', 'integer'), 
    Field('ciptadent_maxi_12_penjualan', 'integer'), 
    Field('mama_lemon_stock', 'integer'), 
    Field('zinc_refreashing_cool_stock', 'integer'), 
    Field('zinc_black_shine_stock', 'integer'), 
    Field('systema_spring_fresh_stock', 'integer'), 
    Field('systema_mentol_breeze_stock', 'integer'), 
    Field('emeron_anti_dandruff_stock', 'integer'), 
    Field('emeron_black_shine_stock', 'integer'), 
    Field('ciptadent_maxi_12_stock', 'integer'), 
    Field('total_transaksi_dalam_satuan_rupiah', 'integer'), 
    Field('foto_bukti_trasnsaksi'), 
    Field('foto_display_produk_1'), 
    Field('foto_produk_dislay_2'), 
    Field('foto_posm_1'), 
    Field('foto_posm_2'), 
    #auth.signature,
    on_define = on_define_lion, 
    format = '%(uuid)s')

这里是我的模块:

#!/usr/bin/env python
# coding: utf8
from gluon import *

# represent
represent_integer = lambda value, field: \
    SPAN('%s' % (format(value, ",d").replace(",", ".") ), 
         _class = 'text-success') if value > 0 else SPAN(0, _class = 'text-danger')
represent_url = lambda value, field: A('View', _target="_blank", _href="%s" % value) if value else ''

# requires
requires_date = lambda: \
    IS_DATE(format = current.T('%Y-%m-%d'), error_message = current.T('Enter date as yyyy-mm-dd') )

requires_set_not_empty = lambda list_value: IS_IN_SET(list_value, zero = current.T('Choose One') )

# widget
widget_string = lambda field, value: \
    SQLFORM.widgets.string.widget(field, value, 
                                  _class = "form-control string", 
                                  _placeholder = current.T('Should be filled') )

我想创建一个这样的控制器:

选择日期

开始日期:_________

结束日期:__________

提交(按钮)

所以在我按下提交按钮后,我想要一个表来查看日期开始和日期结束之间的数据库。帮助我完全卡住,谢谢你

------ U P D A T E D ------

我在我的控制器中使用此代码,

 def non_register(): 
    form = SQLFORM.factory(
    Field("start_date", type="date"),
    Field("end_date", type="date"))
    if form.process().accepted:
        q = ((db.lion.date_of_response >= form.vars.start_date) &
             (db.lion.date_of_response <= form.vars.end_date))
    rows = db(q).select()
    return dict(form=form,rows=rows)

但显示错误

<type 'exceptions.UnboundLocalError'> local variable 'q' referenced before assignment

然后如何显示此表单中的行?将是答案的答案,谢谢你

1 个答案:

答案 0 :(得分:0)

您可以使用SQLFORM.factory创建所需的表单。

form = SQLFORM.factory(
    Field("start_date", type="date"),
    Field("end_date", type="date"))

这会生成不错的日期选择器,form.vars.start_dateform.vars.end_date都是datetime.date个实例,您可以直接在DAL查询中包含这些实例。

q = ((db.yourtable.date >= form.vars.start_date) &
     (db.yourtable.date <= form.vars.end_date))
rows = db(q).select()

希望这有帮助!