如何修复过滤器和分页



我正在尝试根据prince范围筛选项目,然后对结果进行分页。我已经看了文件,但还是没用。我已经尝试了我能想出的所有逻辑,但仍然没有奏效。

搜索会显示第一页的项目,但一旦我点击后续页面,它就不会显示任何内容。如果我使用if request.Method==''POST:",它显示:variable used before assignment

我也尝试过使用if request.method =="GET",但它仍然不起作用。搜索会显示第一页的项目,但一旦我点击后续页面,它就不会显示任何内容。

型号.py

class Property(models.Model):
name =models.CharField(max_length=200) 
price = models.IntegerField(default=1000)
bedroom = models.IntegerField(default=1)
bathroom =models.IntegerField(default=1)
status =models.CharField(max_length=200,blank=True,null=True) 
sqft =models.CharField(max_length=10,blank=True,null=True) 
acre =models.CharField(max_length=10,blank=True,null=True) 
Location = models.CharField(max_length=200,blank=True,null=True) 
describe =models.CharField(max_length=200,blank=True,null=True)
img = CloudinaryField(blank=True,null=True)


def __str__(self):
return self.name 

class Meta:
#db_table='accommodation' 

verbose_name_plural='Property'

view.py-我不知道这里是否有错误,因为除了第一页之外,它没有显示任何结果

def availableProperty(request):
#if request.method =="POST":

name =request.POST.get('property')
minpay =request.POST.get('min-price')
maxpay =request.POST.get('max-price')

if name == 'all':
#getiing the model property and filtering the price range
result= Property.objects.filter(price__range=(minpay, 
maxpay))#.order_by('-price')

else:
result= Property.objects.filter(price__range=(minpay, 
maxpay),name=name)#.order_by('-price')


p = Paginator(result,5)
number = request.GET.get('page') 
resultobj = p.get_page(number)

# here ,i used try excerpt yet it didn't work
#   try:
#resultobj = p.page(number)
#      resultobj = p.get_page(request.GET.get('page'))


# except PageNotAnInteger:
#            resultobj = p.get_page(1)
# except EmptyPage:

#      resultobj = p.get_page(p.num_pages)   

context ={
'resultobj':resultobj
}
return render(request, 'property_info.html',context) 



template-property_info.html

{% extends 'index.html' %}
{% load static %}
{% block content %}



<style>

.want_to_buy{
display: none;
}
.display{
display: none;
}
.property-search{
display: none;
}
.featured{
display: none;
}


.flex-container {
display: flex;
flex-wrap: wrap;
margin-top: 25px;
}

.flex-item-left {
flex: 50%;
}

.flex-item-right {
flex: 50%;
}
.card-image{
width: 300px;
height: 280px;
margin-left: 20px;
}
.card-image img{
width: 100%;
height: 100%;
}
.property-information{
margin-left: 20px;
}
.property-address h2{
font-size: 20px;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
font-weight: 400;

color: #0076AD;
margin-left: 10px;
}
.property-information h3{
font-size: 18px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
color: #333;

}
.property-information span{
color: green;
}
.right{
margin-left: 50px;
}
.search-result{
margin-left: 30px;
}
.search-section .description{
font-size: 15px;
color: #333;
font-family: Verdana, Geneva, Tahoma, sans-serif;
margin-top: 30px;
margin-left: 30px;
}
.btn-groupy{
margin-top: 5px;
margin-bottom: 70px;
margin-left: auto; 
margin-right: 20;
right: -50px;


}
.contar{
margin-left: 30px;
}

/* Responsive layout - makes a one column layout instead of a two-column layout */
@media (max-width: 800px) {
.flex-item-right, .flex-item-left {
flex: 100%;
}
}
</style>






<div class="search-section">

<h1 class="search-result">Search result</h1>

{% for result in resultobj %} 
<div class="flex-container">


<div>


<div class="property-flex">

<a href="">
<div class="card-image ">
<img src="{{ result.img.url }}" alt="">
</div>
</a>
</div>
</div>
<div>
<div class="property-information">
<a href="">
<div class="property-address">
<h2>{{result.Location }}</h2>
</div>
</a>
<div class="property-information">
<h3>Price:<span>${{result.price }}</span></h3>
<h3>bedroom:{{result.bedroom }}</h3>
<h3>bathroom:{{result.bathroom }}</h3>
<h3>Sqft:{{result.sqft }}</h3>
<h3>Acres:{{result.acre}}</h3>
<h3>SubdivisionSee Legal Description Below.</h3>

</div>
</div>
</div>
<div class="property-information">
<h3 class="right">Listing ID50044316
PBMLS-RETS</h3>
</div>  
</div>
<h3 class="description"> {{result.describe }}</h3>
<div class="contar">
<div class="btn-groupy " role="group" aria-label="Basic mixed styles example">
<a href="" type="button" class="btn btnb btn-danger">View pictures</a>
<a href="" type="button" class="btn btnb btn-warning">Visual Tour</a>
<a href="" type="button" class="btn btnb btn-success">Add To Favourite</a>
<a href="" type="button" class="btn btnb btn-dark">View Details</a>
</div>
</div>
{% endfor %}
</div>




{% if resultobj.has_other_pages %}
<ul class="pagination">
{% if resultobj.has_previous %}
<li><a href="?page={{ resultobj.previous_page_number }}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
{% for i in resultobj.paginator.page_range %}
{% if resultobj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if resultobj.has_next %}
<li><a href="?page={{ resultobj.next_page_number }}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
{% endif %}

{% endblock %}

# urls.py 
path('property_search/' ,views.availableProperty,name='property_search')

对我来说,一个快速的解决方案是使用javascript而不是django/python,因为我使用的是:

<a class="page-link" data-href="?page={{items.paginator.num_pages}}" href="?page={{items.paginator.num_pages}}">Last</a>

我添加了一个javascript来检测是否有过滤器,并添加&page=";页码";而不是page=";页码";。

<script>
// pagination
let pag = document.querySelectorAll('a.page-link')
console.log(pag.length)
let url = window.location
if (url.href.includes("?csrfmiddlewaretoken"))
{
for (let i = 0; i < pag.length; i++)
{
pag[i].href = url.search + pag[i].getAttribute("data-href").replace("?","&")
console.log(pag[i].getAttribute("data-href"))
}
} else {
console.log("not included")
}
</script>

希望能有所帮助。

最新更新