我正在尝试从Django获取媒体,但在控制台xhr.js?1a5c:244中收到404未找到错误消息
GET http://127.0.0.1:8000/api/v1/products/$%7Bcategory_slug%7D/$%7Bproduct_slug%7D 404 (Not Found)
我的产品/urls.py代码是这样的
from product import views
urlpatterns = [
path('latest-products/', views.LatestProductsList.as_view()),
path('products/<slug:category_slug>/<slug:product_slug>/', views.ProductDetail.as_view()),
]
第一个路径运行良好,但第二个路径显示以下错误:Get http://127.0.0.1:8000/api/v1/products/$%7Bcategory_slug%7D/$%7Bproduct_slug%7D 404(Not Found)
我的产品/views.py是
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Product
from .serializers import ProductSerializer
class LatestProductsList(APIView):
def get(self, request, format=None):
products = Product.objects.all()[0:4]
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)
class ProductDetail(APIView):
def get_object(self, category_slug, product_slug):
try:
return Product.objects.filter(category__slug=category_slug).get(slug=product_slug)
except Product.DoesNotExist:
raise Http404
def get(self, request, category_slug, product_slug, format=None):
product = self.get_object(category_slug, product_slug)
serializer = ProductSerializer(product)
return Response(serializer.data)
我的index.js路由器代码是
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Product from '../views/Product.vue'
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
},
{
path: '/:category_slug/:product_slug/',
name: 'Product',
component: Product
}
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
})
export default router
1条答案
按热度按时间hgqdbh6s1#
终结点的格式应为
http://127.0.0.1:8000/api/v1/products/test/test/
或http://127.0.0.1:8000/api/v1/products/products/test/test/
,其中应将test
值替换为正确的段塞值。您正在尝试将${category_slug}
作为文本而不是其值进行传递。请检查Vue项目中的URL构造函数。