如何在日期字段

时间:2018-04-24 01:05:35

标签: python html django

我有一个dat_transacao字段,我想用它创建一个范围,但它不起作用。我在dat_inicial中创建了dat_finalfilters.py,但屏幕未按这些字段过滤记录。有些事情是错的,但我不知道它是什么。有人可以帮我吗? 感谢。

models.py:

class transacao(models.Model):
    dat_transacao = models.DateTimeField('Geração da Transação', auto_now=True)
    pessoa = models.ForeignKey(pessoa, default='', blank=True, null=True) # Chave estrangeira da Classe Pessoa
    tipotransacao = models.ForeignKey(tipotransacao, default='', blank=True, null=True) # Chave estrangeira da Classe Tipo de Transação
    servico = models.ForeignKey(servico, default='', blank=True, null=True) # Chave estrangeira da Classe Servico (que pode ser NULL)
    bloco = models.ForeignKey(bloco, default='', blank=True, null=True) # Chave estrangeira da Classe Bloco

filters.py:

from .models import *
import django_filters
#from django_filters import DateTimeFilter

class livrorazaoFilter(django_filters.FilterSet):

    dat_inicial = django_filters.DateTimeFilter(name='dat_transacao',lookup_type=('gte'))
    dat_final = django_filters.DateTimeFilter(name='dat_transacao',lookup_type=('lte'))

    class Meta:
        model = transacao
        fields = ['dat_inicial', 'dat_final', 'tipotransacao', 'pessoa', 'bloco']

views.py

def transacao_livrorazao(request, id=None):
    instancia_transacao = transacao.objects.all().order_by('-dat_transacao')
    instancia_transacaofilter = livrorazaoFilter(request.GET, queryset=instancia_transacao)
    context = {
            "queryset": instancia_transacao,
            "filter": instancia_transacaofilter,
            "instancia_transacao": instancia_transacao
    }
    return render(request, 'livrorazao/livrorazao.html', context)

livrorazao.html:

<!DOCTYPE html>
<html dir="ltr">

        <form class=" bd-form-2 " action="#" name="form-name" method="get">
            {% csrf_token %}

        <p>Período</p>
        <p>De</p>
            <input type="date" name='filter.form.dat_inicial' id="dat_inicial"  class="bd-form-input" >
        <p>até</p>
            <input type="date" name='filter.form.dat_final' id="dat_final"  class="bd-form-input" >
        <p>Tipo de Transação</p>
             {{ filter.form.tipotransacao }}

         <button type="submit">Consultar</button>
        </form>

        <br>
        <table width="100%" border="0" cellspacing="1" cellpadding="2">
        <tbody><tr>
            <th>ID Transação</th>
            <th>Data/Hora</th>
            <th>TipoTransação</th>
            <th>Blockchain</th>
        </tr>

      {% for instance in filter.qs  %}
        <tr>
            <td>{{ instance.id }}</td>
            <td>{{ instance.dat_transacao }}</td>
            <td>{{ instance.tipotransacao }}</td>
            <!--<td>{{ instance.pessoa }}</td>-->
            <td>{{ instance.bloco }}</td>
        </tr>
      {% endfor %}

</tbody></table>
</html>

2 个答案:

答案 0 :(得分:1)

我的解决方案类似于@cezar发布的内容。但是,将attrs={'class': 'datepicker'}更改为attrs={'type': 'date'}对我来说很有效。

我的前端表单显示是使用django-widget-tweaks

管理的

以下是对我有用的内容的摘录:

models.py

class Applications(models.Model):
    #...
    status = models.CharField(max_length=10, choices=STATUS)
    start_date = models.DateTimeField(auto_now_add=True)

filters.py

from .models import Applications
import django_filters

class ApplicationFilter(django_filters.FilterSet):
    start_date = django_filter.DateTimeFromRangeFilter(widget=django.widgets.RangeWidget(
    attrs={'type': 'date'}))

    class Meta:
        model = Applications
        fields = ['status', 'start_date']

views.py

from django.shortcuts import render
from .models import Applications
from .filters import ApplicationFilter

def application_search(request):
    #... I stripped the view function down to what concerns filters.py
    application_list = Application.objects.all()
    application_filter = ApplicationFilter(request.GET, queryset=application_list)

    return render(
        request,
        "applications.html,
        {'filter': application_filter}
    )

applications.html(带有引导程序)

...
{% load bootstrap4 %}{% load widget_tweaks %}
<form method="GET" class="form-inline">
<div class=form-group>
<label style="margin-right: 20px">Filter: </label>
{% render_field filter.form.status class="form-control"%}
<label style="margin-left: 20px; margin-right: 10px;">From </label>
{% render_field filter.form.start_date class="form-control" %}
<button style="margin-left: 10px" class="btn btn-primary btn-md">
 GO
</button>
</div>
</form>
...

PS:默认情况下,我已将django_filters添加到settings.py

Here's how the form looks

答案 1 :(得分:0)

您可以使用DateFromToRangeFilter。而不是dat_inicialdat_final将此代码段添加到FilterSet类:

dat_transacao = django_filters.DateTimeFromToRangeFilter()

此过滤器应创建两个输入字段,并查询值在下限和上限之间的条目。通过传递更多参数,您可以修改外观和行为,例如添加datepicker:

dat_transacao = django_filters.DateTimeFromToRangeFilter(
    widget=django_filters.widgets.RangeWidget(
        attrs={'class': 'datepicker'}
    )
)

根据需要调整HTML模板。基本上它应该适用于这些行:

<form class="bd-form-2 " action="" name="form-name" method="get">
    {% csrf_token %}
    {{ filter.form }}
    <button type="submit">Consultar</button>
</form>