我有一个模板,以表格格式显示用户数据(日期,姓名,地址,联系人号码)。如何在此模板上应用过滤器按钮,该按钮用于接收用户日期并修改表以显示该特定日期的条目。
Table.html
{% block content %}
<h1 style="font-size:300%;">ATTENDANCE TABLE</h1><hr /><br />
<table>
<tr>
<th>DATE</th>
<th>NAME</th>
<th>SITE</th>
<th>TYPE OF LEAVE</th>
<th>REASON</th>
<th>ARRIVAL</th>
<th>TIME IN</th>
<th>TIME OUT</th>
</tr>
{% for topic in topics %}
<tr>
<td>{{ topic.date }}</td>
<td>{{ topic.name }}</td>
<td>{{ topic.site }}</td>
<td>{{ topic.leave_type }}</td>
<td>{{ topic.reason }}</td>
<td>{{ topic.arrival }}</td>
<td>{{ topic.intime }}</td>
<td>{{ topic.outtime }}</td>
</tr>
{% endfor %}
显示此表后,我希望有一个过滤器按钮接受日期并修改表以显示该输入日期的条目。我是否必须创建一个新视图或只是使用.html文件?
我尝试使用{{variable | date}}过滤器无效。
编辑: 共享我的其他文件以实现表单模板过滤器方法。
forms.py
from django import forms
from django.forms.fields import DateField, TimeField, DateInput, TimeInput,
datetime
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext_lazy as _
from django.forms.extras.widgets import SelectDateWidget
from .lala import SelectTimeWidget
import datetime
from .models import Topic, AppStaff
class TopicForm(forms.ModelForm):
date= forms.DateField(widget=SelectDateWidget, initial=datetime.date.today)
name=forms.ModelChoiceField(queryset=AppStaff.objects.values_list('agent',flat=True))
intime=forms.TimeField(widget= SelectTimeWidget(twelve_hr=True, required=False,use_seconds=False),required=False)
outtime=forms.TimeField(widget= SelectTimeWidget(twelve_hr=True, required=False,use_seconds=False),required=False)
class Meta:
model = Topic
#fields = ['site','leave_type','reason','arrival','intime','outtime']
class FilterForm(forms.Form):
date1= forms.DateField(widget=SelectDateWidget, initial=datetime.date.today)
Views.py
from django.utils import timezone
from django.shortcuts import render
from django.http import HttpRequest
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from .models import Topic, AppStaff
from .forms import TopicForm, FilterForm
import datetime
def topics(request):
"""Show all topics."""
topics = Topic.objects.all()
context = {'topics': topics}
return render(request, 'app/topics.html', context)
def new_topic(request):
"""Add a new topic."""
if request.method != 'POST':
# No data submitted; create a blank form.
form = TopicForm()
else:
# POST data submitted; process data.
form = TopicForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('new_topic'))
context = {'form': form}
return render(request, 'app/new_topic.html', context)
def filter(request):
form = FilterForm()
if request.method == "POST":
f = FilterForm(request.POST)
if f.is_valid():
posts = Topic.objects.filter()
return HttpResponseRedirect("app/filter.html",{"posts":posts})
else:
return render_to_response("app/filter.html",{"form":form} , context_instance = RequestContext(request))
filter.html
<body>
<table>
<tr>
<th>DATE</th>
<th>NAME</th>
<th>SITE</th>
<th>TYPE OF LEAVE</th>
<th>REASON</th>
<th>ARRIVAL</th>
<th>TIME IN</th>
<th>TIME OUT</th>
<th>COMMENT</th>
</tr>
{% for post in posts %}
<tr>
<td>{{ post.date }}</td>
<td>{{ post.name }}</td>
<td>{{ post.site }}</td>
<td>{{ post.leave_type }}</td>
<td>{{ post.reason }}</td>
<td>{{ post.arrival }}</td>
<td>{{ post.intime }}</td>
<td>{{ post.outtime }}</td>
<td>{{ post.comment }}</td>
</tr>
{% endfor %}
</table>
</body>
我的FilterForm显示日期字段,但是当我点击提交时,它应该打开带有已过滤列表的filter.html。我确定我错过了一些东西,但不知道它现在几周是什么。
答案 0 :(得分:1)
Django模板在服务器端创建页面,然后无法控制客户端发生的事情。
如果您想使用Django模板进行过滤,您可以使用表单将操作提交到服务器并使用模板过滤器重新呈现页面。但是,这将是糟糕的用户体验。
我建议使用javascript来实现这一目标。一个好的javascript lib是datatables。对于数据表django-datatables,还有一个django应用程序。
完全披露:我将这个库用于非常复杂的表格,并为此做出了贡献。