0

我不知道如何重定向到类别列表页面,但 url 工作正常我只是无法通过 id 将正确的重定向 url 添加到 href 标记中:(

html

<div class="widget widget-catgs">
  <h3 class="widget-title">Categories</h3>
  <ul>

    {% for cat in category %}
    <li>
      <a href="**{% url 'category-list' property.category.id %}**" title=""><i
          class="la la-angle-right"></i><span>{{ cat.category__title }}</span></a>
      <span>{{ cat.category__title__count }}</span>
    </li>
    {% endfor %}

  </ul>
</div>

视图.py

def property_by_category(request, category_id):
    property_list = Property.objects.all()
    category = get_object_or_404(Category, pk=category_id)
    category_list = Property.objects.filter(category=category)

    context = {
        'category_list': category_list,
        'category_name': category,
        'property': property_list
    }
    return render(request, 'property/property-category.html', context)

网址.py

urlpatterns = [
    path('', views.property, name='property-page'),
    path('details/<int:property_id>', views.property_details, name='property-details'),
    path('category/<int:category_id>', views.property_by_category, name='category-list'),
    path('add-property/', views.add_property, name='add-property'),
]

我正在学习django,请看一下我的代码。

4

0 回答 0