在 django 中按升序/降序对过滤列表进行排序
我使用 Django 创建了一个员工管理系统。我在其中完成了一种过滤方法,并且基于从下拉菜单和文本输入中选择的选择。过滤工作正常。在第一页上,它给出了整个员工列表,可以按升序和降序显示。在同一页面上给出了过滤方法。过滤后的数据显示在另一个页面中。现在我想在过滤后的数据页面上提供一个按钮,单击该按钮以升序/降序显示数据。我编写了一个单独的函数,用于对完整员工列表的视图进行升序和降序。如何使用它来实现此功能。我将在这里粘贴我的代码。请帮助我找到解决方案,因为我是 django 编程新手。
我已经给出了 2 个单独的图像进行升序和降序。我想要这样:按升序单击 1 个图像列表;然后单击其他图像按降序列出它。
Filter()
def filter(request):
val3=''
if request.GET.has_key('choices'):
val2=request.GET.get('choices')
if request.GET.has_key('textField'):
val3=request.GET.get('textField')
if request.POST:
val2=request.POST.get('choices')
val3=request.POST.get('textField')
if val2=='Designation':
newData = EmployeeDetails.objects.filter(designation=val3)
flag=True
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName__icontains=val3)
flag=True
elif val2=='EmployeeID':
newData = EmployeeDetails.objects.filter(employeeID=val3)
flag=True
elif val2=='Project':
newData = EmployeeDetails.objects.filter(project=val3)
flag=True
elif val2=='DateOfJoin':
newData = EmployeeDetails.objects.filter(dateOfJoin=val3)
flag=True
else:
return HttpResponseRedirect('/employeeList/')
#tableList = EmployeeDetails.objects.all()
paginator = Paginator(newData, 10)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('filter.html',{'newData':newData,'emp_list': contacts,'val2':val2,'val3':val3,'flag':flag})
filter.html
<div>
Employees List
<a STYLE="text-decoration:none" align=center href="http://10.1.0.90:8080/sortAscend/ "> <img src="/static/sort_asc.gif " border="1" height="12" /> </a>
<h4 align="left">
{%for data in newData%}
<a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{data.id}}?choices={{val2}}&textField={{val3}}&flag=1 ">
{{ data.userName}}<br>
{%endfor%}
</h4>
</div>
升序和降序函数
def sortAscend(request):
tableList = EmployeeDetails.objects.all().order_by('userName')
paginator = Paginator(tableList, 12)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('sortAscend.html', {'emp_list': contacts})
#Method for listing the employees in descending order
def sortDescend(request):
tableList = EmployeeDetails.objects.all().order_by('-userName')
paginator = Paginator(tableList, 12)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('sortDescend.html', {'emp_list': contacts})
sortAscending.html
{%for emp in emp_list.object_list%}
<tr> <td><a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{emp.id}} "> {{ emp.userName }} </a></td> </tr><td>
{%endfor%}
I have created an Employee management system using Django. I have done a filtering method in it and is based on a choice selected from a drop down menu and a text input. the filtering is working fine. On the first page it gives the entire employee list which can be shown in both ascending and descending order. On the same page is given the filtering method. The filtered data is shown in another page. Now i want to give a button on the filtered data page, clicking on that button shows the data in ascending/descending. I have written a separate function for ascending and descending in views for the full employee listing. How can it be used for this functionality. I will paste my code here. Please help me to find a solution as i am new to django programming.
I have given 2 separate images for ascending and descending.I want it this way: Clicking on 1 image lists in ascending order; and clicking on other image lists it in descending order.
Filter()
def filter(request):
val3=''
if request.GET.has_key('choices'):
val2=request.GET.get('choices')
if request.GET.has_key('textField'):
val3=request.GET.get('textField')
if request.POST:
val2=request.POST.get('choices')
val3=request.POST.get('textField')
if val2=='Designation':
newData = EmployeeDetails.objects.filter(designation=val3)
flag=True
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName__icontains=val3)
flag=True
elif val2=='EmployeeID':
newData = EmployeeDetails.objects.filter(employeeID=val3)
flag=True
elif val2=='Project':
newData = EmployeeDetails.objects.filter(project=val3)
flag=True
elif val2=='DateOfJoin':
newData = EmployeeDetails.objects.filter(dateOfJoin=val3)
flag=True
else:
return HttpResponseRedirect('/employeeList/')
#tableList = EmployeeDetails.objects.all()
paginator = Paginator(newData, 10)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('filter.html',{'newData':newData,'emp_list': contacts,'val2':val2,'val3':val3,'flag':flag})
filter.html
<div>
Employees List
<a STYLE="text-decoration:none" align=center href="http://10.1.0.90:8080/sortAscend/ "> <img src="/static/sort_asc.gif " border="1" height="12" /> </a>
<h4 align="left">
{%for data in newData%}
<a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{data.id}}?choices={{val2}}&textField={{val3}}&flag=1 ">
{{ data.userName}}<br>
{%endfor%}
</h4>
</div>
ascending and descending functions
def sortAscend(request):
tableList = EmployeeDetails.objects.all().order_by('userName')
paginator = Paginator(tableList, 12)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('sortAscend.html', {'emp_list': contacts})
#Method for listing the employees in descending order
def sortDescend(request):
tableList = EmployeeDetails.objects.all().order_by('-userName')
paginator = Paginator(tableList, 12)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('sortDescend.html', {'emp_list': contacts})
sortAscending.html
{%for emp in emp_list.object_list%}
<tr> <td><a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{emp.id}} "> {{ emp.userName }} </a></td> </tr><td>
{%endfor%}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在视图级别处理排序的另一种替代方法是在模板上进行排序。因此,您可能需要查看 jquery tablesorter (因为您也在显示中使用表格) 。它处理按升序/降序排序。
因此,如果过滤后的结果已准备好显示到页面上,例如filtered_results.html,则可以这样做。
单击“Some-Label”的单元格将按升序/降序切换排序。
此外,它还有一个用于处理分页的插件。查看此演示的链接。
Another alternative to handling sorting in the view level is to do it on the templates. For this reason, you might want to checkout jquery tablesorter (since you are using a table in the display as well). It handles sorting in ascending/descending order.
So if you have the results after filtering that is ready to be displayed to a page, say filtered_results.html, you can do it like this.
Clicking on the cell of 'Some-Label' will toggle the sorting in ascending/descending order.
Furthermore, it has a plugin for handling pagination. Check out this link for the demo.
我不确定我是否明白这个问题,但是如果您想将排序应用于过滤后的对象,您可能需要添加某种缓存(与 django 的内置缓存不同),它存储过滤后的查询集并对其进行排序。或者您可以使用 django 的会话管理传递过滤器选项并重做排序查询。这将需要重构过滤器,以便 if/elif 链独立于该视图并返回过滤后的查询集。
例如:
现在您的排序方法可以获取最初传递的过滤器值。您也可以选择将它们作为获取参数添加到视图 url 中。
I'm not sure I get the question, but if you want to apply the sorting to the filtered objects, you might want to add some sort of caching (different from django's builtin caching), that stores the filtered queryset and sort that. Or you can pass the filter option around using django's session management and redo the sorting query. This would require to refactor filter so the if/elif chain is independant of that view and returns the filtered queryset.
ex:
Now your sorting methods can get the filter values that was passed originally. Optionally you can add them as get parameters to the views url also.