You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django三明治店API:Postman请求指定产品返回404问题排查

解决Django获取指定产品返回404的问题

你的问题核心是URL路由配置和请求方式不匹配,再加上视图代码的小疏漏,咱们一步步来修复:

1. 先搞定404的根源:URL路由和请求地址不匹配

你在Postman里用的是带**查询参数(query string)**的请求:http://10.58.1.157:8000/product/sandwich?product_id=1,但你写的两个URL配置都是把product_id放在URL路径里(比如sandwich/1/这种格式),这就导致Django找不到对应的路由,直接返回404。

两种修正方案的URL配置

方案A:继续用查询参数传值

如果想保留?product_id=1的传参方式,你的urls.py应该改成这样(不需要在路径里加product_id):

from django.urls import path
from .views import ProductView

urlpatterns = [
    path('sandwich/', ProductView.as_view()),  # 只匹配/sandwich/路径,参数从query string读取
]

方案B:改用路径参数传值(更符合RESTful风格)

如果想把请求地址改成http://10.58.1.157:8000/product/sandwich/1/这种格式,URL配置用Django 2.0+的语法:

from django.urls import path
from .views import ProductView

urlpatterns = [
    path('sandwich/<int:product_id>/', ProductView.as_view()),  # 路径参数写法
]

2. 修正视图(views.py)的代码问题

不管选哪种传参方案,你的当前视图都有两个小问题:

  • Product.objects.get(id=product_id).values()是错误写法:get()返回单个模型对象,不能直接调用values(),得把对象转成字典或者用filter()
  • 没有处理参数为空、产品不存在的异常,容易抛出500错误

对应方案A的视图修正(查询参数)

import json
import bcrypt
import jwt
from django.views import View
from django.http import JsonResponse
from django.core.exceptions import ObjectDoesNotExist
from .models import Product, Category, SubCategory

class ProductView(View):
    def get(self, request):
        product_id = request.GET.get('product_id', None)
        
        # 处理product_id为空的情况
        if not product_id:
            return JsonResponse({'error': 'product_id参数不能为空'}, status=400)
        
        try:
            # 获取产品并转成字典返回(根据你的Product模型字段调整内容)
            product = Product.objects.get(id=product_id)
            product_data = {
                'id': product.id,
                'product_name': product.product_name,
                # 这里添加你需要返回的其他字段,比如价格、描述等
            }
            return JsonResponse({'product': product_data})
        except ObjectDoesNotExist:
            # 产品不存在时返回明确的错误
            return JsonResponse({'error': '指定产品不存在'}, status=404)

对应方案B的视图修正(路径参数)

如果用路径参数,需要从kwargs里获取product_id:

import json
import bcrypt
import jwt
from django.views import View
from django.http import JsonResponse
from django.core.exceptions import ObjectDoesNotExist
from .models import Product, Category, SubCategory

class ProductView(View):
    def get(self, request, product_id):  # 新增路径参数product_id
        try:
            product = Product.objects.get(id=product_id)
            product_data = {
                'id': product.id,
                'product_name': product.product_name,
                # 其他字段...
            }
            return JsonResponse({'product': product_data})
        except ObjectDoesNotExist:
            return JsonResponse({'error': '指定产品不存在'}, status=404)

3. 测试验证

  • 方案A:保持Postman的请求地址http://10.58.1.157:8000/product/sandwich?product_id=1,发送GET请求即可
  • 方案B:把Postman的请求地址改成http://10.58.1.157:8000/product/sandwich/1/,发送GET请求

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:47:47