我已经尝试了几天,以筛选具有类别模型的类别字段的一系列帖子,以便仅在模板中显示与特定类别相对应的结果,这与我们在e中看到的类似-商业。想法是在一个下拉按钮中显示类别,从而能够过滤帖子。
谁能帮助我非常感谢您。
models.py
class Category(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=40, unique=True)
is_active = models.BooleanField(default=True)
class Meta:
verbose_name_plural = "Categories"
def __str__(self):
return self.name
def get_absolute_url(self):
return "categoria/%s/" % self.name
class SaleProperty(models.Model):
VENDER = 'VENDER'
RENTAR = 'RENTAR'
SI = 'SI'
NO = 'NO'
CHOICES = (
(VENDER, 'Venta'),
(RENTAR, 'Alquilar')
)
PARKING_OPTIONS = (
(SI, 'Si'),
(NO, 'No')
)
ROOMS = [(i, i) for i in range(11)]
user = models.ForeignKey(User, on_delete=models.CASCADE)
categories = models.ForeignKey('Category', on_delete=models.CASCADE)
title = models.CharField(max_length=100)
description = models.TextField()
parking_option = models.CharField(max_length=2, choices=PARKING_OPTIONS, default=NO)
rent_or_sale = models.CharField(max_length=10, choices=CHOICES, default=VENDER)
rooms = models.IntegerField(choices=ROOMS)
bathrooms = models.IntegerField(choices=ROOMS)
price = models.DecimalField(max_digits=10, decimal_places=1)
area = models.FloatField(max_length=7)
pub_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title # Shows admin the name of entry blog
def summary(self):
return self.description[:100] # This makes short description
def publish(self):
self.pub_date = timezone.now()
self.save()
def pub_date_pretty(self):
return self.pub_date.strftime('%B %-m %Y %X')
def get_images(self):
return UploadImage.objects.filter(post=self)
def get_first_image(self):
return UploadImage.objects.filter(post=self)[:1][0]
views.py
def public_list(request):
list = SaleProperty.objects.filter(pub_date__lte=timezone.now()).order_by('pub_date')
paginator = Paginator(list, 10) # Show 10 items per page
page = request.GET.get('page')
item = paginator.get_page(page)
args = {'list': list, 'item': item}
return render(request, 'realstate/public_list.html', args)
从应用程序urls.py
urlpatterns = [
path('vender/', views.sale_property, name='sale_property'),
path('<int:id>/', views.post_property_detail, name='post_property_detail'),
path('user-listado/', views.post_list_user, name='post_list_user'),
path('editar/<int:id>/', views.edit_post, name='edit_post'),
path('borrar_publicacion/<int:id>/', views.delete_post, name='delete_post'),
path('listado_pub/', views.public_list, name='public_list'),
]
我要由任何用户过滤的public_list.html模板
{% extends 'base.html' %}
{% load static %}
{% block 'content' %}
<div>
<div class="btn-group" role="group" aria-label="Button group with nested dropdown">
<div class="btn-group" role="group">
<button id="btnGroupDrop1" type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
Categorias
</button>
<div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
{% for cat in list %}
<a class="dropdown-item" href="{{ cat.categories}}">{{cat.categories.name}}</a>
{% endfor %}
</div>
</div>
</div>
<!-- Page Heading -->
<h4 class="my-4">Propiedades Disponibles
<hr/>
<small>Listado de propiedades en el mercado</small>
</h4>
<div>
<div>
<div>
<!-- Single Featured Property -->
{% for post in item %}
{% include "realstate/_property_thumbnail.html" %}
<br/>
<br/>
<hr/>
{% endfor %}
</div>
</div>
{% include "realstate/_paginator.html" %}
</div>
<br/>
</div>
{% endblock %}
我通过这种方式尝试解决了上一个问题,由于在URL中使用段塞时,由于应用程序URL中的冲突,因此无法正常工作,因此我附加了项目URL以及部分内容属于我的问题所在的基地。
项目中的urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('accounts/', include('accounts.urls', namespace='accounts')),
path('property/', include('property.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
base.html部分代码以及指向项目列表的链接,是以前的解决方案中的问题,因为应用程序中的urls.py出现了问题。
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="{% url 'public_list' %}"> #this is the part if the problem with slug in the urls.py in app dir
<button type="button" class="btn btn-secondary">Publicaciones</button>
</a>
</li>
这是我上一篇有关堆栈溢出的文章。