我正在开发一个Django博客,并为详细信息页面实现了slug。我偶然发现了一个问题。
找不到页面(404)请求方法:GET请求
网址:http://127.0.0.1:8000/susu/已提升
by:myshop.views.product_detail
没有产品与给定的查询匹配。
这是我的 myshop / urls.py 文件
from django.conf.urls import url
from . import views
app_name = 'myshop'
urlpatterns = [
url(r'^$', views.product_list, name='product_list'),
url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
]
这是我的 urls.py 文件
from django.contrib import admin
from django.conf.urls import url, include
from myshop import views as mv
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('myshop.urls', namespace='myshop')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是我的 views.py 文件
from django.shortcuts import render, get_object_or_404
from .models import Category, Product
# Create your views here.
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request, 'shop/product/list.html', {'category': category, 'categories': categories, 'products': products})
def product_detail(request, product_slug):
product = get_object_or_404(Product, slug=product_slug, available=True)
return render(request, 'shop/product/detail.html', {'product': product})
这是我的 models.py 文件
from django.db import models
from django.urls import reverse
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('myshop:product_list_by_category', args=[self.slug])
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products', on_delete=None)
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('myshop:product_detail', args=[self.slug])
我尝试在网址格式中重新排序网址条目 从
urlpatterns = [
url(r'^$', views.product_list, name='product_list'),
url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
]
到
urlpatterns = [
url(r'^$', views.product_list, name='product_list'),
url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
]
,结果是我可以加载http://127.0.0.1:8000/susu/页面,(susu是类别) 但我无法加载http://127.0.0.1:8000/susu-vanilla/页面,(susu-vanilla 产品) 这是错误
找不到页面(404)请求方法:GET请求
网址:http://127.0.0.1:8000/susu-vanilla/已提升
by:myshop.views.product_list
没有类别与给定的查询匹配。
有人能帮助我吗? 抱歉我的英文不好,谢谢
答案 0 :(得分:2)
您不能拥有这两种网址格式。 Django总是匹配顶级的。
url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
典型的解决方案是为一个(或两个)正则表达式添加前缀,例如:
url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
url(r'^categories/(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),