我已经尝试过几天来过滤类别字段的一系列帖子,这些帖子具有类别模型,仅在模板中显示与特定类别对应的结果,类似于我们在电子商务中看到的结果 . 我们的想法是在下拉按钮中显示类别,从而能够过滤帖子 .

谁能帮助我,非常感谢你 .

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 from the app

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 Template i want to filter by any user

{% 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" %}
                

<hr/> {% endfor %} </div> </div> {% include "realstate/_paginator.html" %} </div>
</div> {% endblock %}

我通过这种方式尝试了解决上一个问题的解决方案并且它不起作用,因为在网址中使用slug时应用程序的url发生冲突,所以我附上了我的项目网址以及我所在的基础部分部分问题 .

urls.py from the project

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 partial code with the link to go to the list of items, is the issue en previous solution because a problem with slug in urls.py in app.

<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>

This is my previous post in stack overflow.

[https://stackoverflow.com/questions/53621642/django-template-filter-by-user-using-category-dropdown-button][1]