2017-05-20 5 views
-1

ich bin fest über meine Anwendung. Ich möchte spezifische Daten aus der Datenbank nach Datum erstellen, die ich auswählen möchte.generieren Sie eine spezifische Daten nach Datum in web2py

hier meine Modelle:

# -*- 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') 

hier meine Module:

#!/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')) 

Ich möchte einen Controller wie diese erstellen:

Datum wählen

Startdatum: _________

Enddatum: __________

einreichen (Knopf)

so, nachdem ich den Absenden-Button drücken i eine Tabelle anzeigen möchten Datenbank zwischen Datum Start- und Enddatum. helfen Sie mir in totaly stecken, danke

------ UPDATE D ------

Im diesen Code in meinem Controller,

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) 

aber ein Fehler zeigen

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

und dann, wie man die Reihen von diesem Formular zeigt? wird über die Antwort freuen, danke

+0

Ihr Modell sieht aus wie das eine oder andere Feld sollte stattdessen eine Referenz sein. –

+0

also muss ich eine andere Tabelle definieren, um das Ergebnis in meinem Formular zu erproben? – alexaxeel

Antwort

0

Sie können SQLFORM.factory verwenden, um die gewünschte Form zu erstellen.

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

Das wird schön Datumsauswahl erzeugen, und beide form.vars.start_date und form.vars.end_date werden datetime.date Instanzen, die Sie direkt in Ihrer DAL Abfrage enthalten.

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

Hoffe, das hilft!

+0

Ihr Code läuft gut, aber die Zeilen nicht angezeigt, – alexaxeel

+0

@alexaxeel Sie setzen die Abfrage an der falschen Stelle. Es sollte wie 'db (query) .select (...)' sein, wie in meinem Beispiel. – cdonts

+0

zeigt es den anderen Fehler, könnten Sie meine aktualisierte Frage überprüfen? habe ich es falsch geschrieben? – alexaxeel