Page not found(404):没有类别匹配给定的查询

  • 本文关键字:查询 found not Page django
  • 更新时间 :
  • 英文 :


下面是我的错误

Page not found(404)
No category matches the given query.
request method: GET
Request URL:    http://127.0.0.1:8000/store/slug/
Raised by:  store.views.product_in_category
Using the URLconf defined in rhizomeedu_prj.urls, Django tried these URL patterns, in this order:
admin/
noticeboard/
markdownx/
store/ [name='product_all']
store/ <slug:category_slug>/ [name='product_in_category']
The current path, store/slug/, matched the last one.

这是Category的模型。

class Category(models.Model):
name = models.CharField(max_length=200, db_index=True)
meta_description = models.TextField(blank=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True, allow_unicode=True)
class Meta:
ordering = ['name']
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('store:product_in_category', args={"slug":self.slug})

views.py的相关部分

def product_in_category(request, category_slug=None):
current_category = None
categories = Category.objects.all()
products = Product.objects.filter(available_display=True)
if category_slug:
current_category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=current_category)
return render(request, 'store/product_list.html',
{'current_category': current_category, 'categories': categories, 'products': products})

这是整个url。py

from django.urls import path, include
from .views import *
app_name = 'store'
urlpatterns = [
path('', product_in_category, name="product_all"),
path('<slug:category_slug>/', product_in_category, name="product_in_category"),
path('<int:id>/<product_slug>/', ProductDetail.product_detail, name="product_detail"),
path('create_product/', ProductCreate.as_view()),
path('cart/', detail, name='detail'),
path('add/<int:product_id>/', add, name='item_add'),
path('remove/<int:product_id>/', remove, name='item_remove'),
path('orders/create/', order_create, name='order_create'),
path('orders/create_ajax/', OrderCreateAjaxView.as_view(), name='order_create_ajax'),
path('orders/checkout/', OrderCheckoutAjaxView.as_view(), name='order_checkout'),
path('orders/validation/', OrderImpAjaxView.as_view(), name='order_validation'),
path('orders/complete/', order_complete, name='order_complete'),
path('admin/order/<int:order_id>/', admin_order_detail, name='admin_order_detail'),
path('admin/order/<int:order_id>/pdf/', admin_order_pdf, name='admin_order_pdf'),
path('cart/', detail, name='detail'),
path('cart/add/<int:product_id>/', add, name='product_add'),
path('cart/remove/<int:product_id>/', remove, name='product_remove'),
]

最后,这是模板product_list.html

的相关部分
<div class="py-3">
<ul class="nav nav-tabs justify-content-center">
<li class="nav-item">
<a class="nav-link {% if not current_category %}active{% endif %}" href="/store/">전체</a>
</li>
{% for c in categories %}
<li class="nav-item">
<a class="nav-link {% if current_category.slug == c.slug %}active{% endif %}" href="{{c.get_absolute_url}}" style="color:black">{{c.name}}</a>
</li>
{% endfor %}
</ul>
</div>
<!-- Section-->
<section class="py-5">
<div class="container px-4 px-lg-5 mt-5">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
{% for p in products %}
<div class="col mb-5">
<div class="card h-100">
<!-- Product image-->
<img class="card-img-top" src="https://dummyimage.com/450x300/dee2e6/6c757d.jpg" alt="..." style="opacity:0.7"/>
<!-- Product details-->
<div class="card-body p-4">
<div class="text-center">
<!-- Product name-->
<h5 class="fw-bolder">{{p.title}}</h5>
<!-- Product price-->
₩{{p.price}}
</div>
</div>
<!-- Product actions-->
<div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
<div class="text-center"><a class="btn btn-outline-dark mt-auto my-1" href="{{ p.get_absolute_url }}">detail</a><a class="btn btn-outline-dark mt-auto my-1" href="#">add to cart</a></div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</section>
</main>
<!-- Bootstrap core JS-->
{% include 'single_pages/footer.html' %}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Core theme JS-->
<script src="{% static 'js/scripts.js' %}"></script>
{% endblock %}

每次我点击"所有"以外的类别,它会引发前面提到的错误。

url也变成了'127.0.0.1:8000/store/slug'这很奇怪,因为没有一个类别叫做"鼻涕虫"。我的代码有什么问题?帮助我。

我猜这是错误的:

def get_absolute_url(self):
return reverse('store:product_in_category', args={"slug":self.slug})

,需要修改为:

def get_absolute_url(self):
return reverse('store:product_in_category', args={"category_slug":self.slug})

检查get_absolute_url

def get_absolute_url(self):
return reverse("store:product_in_category", args=[str(self.slug)])

404页面没有发现错误的原因可能是因为你传递的category_slug中没有类别对象它在这一行显示了错误,

current_category = get_object_or_404(Category, slug=category_slug)

根据Django官方文档,这个问题是由于违反了url和slug的相关规则导致的。当您希望将slug以slug(而不是字符串)的形式放入url时,它必须仅由ASCII组件组成。我的鼻涕虫不是,所以当我改变它时问题就解决了。

最新更新