当前路径search_shop/5ka/与这些路径均不匹配。德扬戈

2024-05-11 03:37:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个错误:

The current path, search_shop/5ka/, didn't match any of these.

问题的原因是slug字段的名称5ka。当我将名称从5ka更改为pka时,效果很好

如何使用5ka而不出错

型号.py

class Shop(models.Model):
    title = models.CharField(max_length=200)
    image = models.ImageField(blank=True)
    slug = models.SlugField(null=False, default="Shop") # I mean this field

def get_search_url(self):
    return reverse('search_shop', kwargs={'slug': self.slug})

url.py

urlpatterns = [
    path('', HomePageView.as_view(), name='main_list'),
    path('search/', SearchResultsView.as_view(), name='search_results'),
    path('search_shop/<slug:slug>', ProductListView.as_view(), name='search_shop'),
    path('search_shop_results/<slug:slug>/', ProductSearchResultsView.as_view(), name='search_shop_results'),
    path('<slug:slug>/<int:pk>/', views.product_detail, name='product_detail'),

    path('shops/', ShopListView.as_view(), name='shop_list'),
]

shop_list.html

{% for shop in shops %}   
    <a href="{{ shop.get_search_url }}"