Django 和 Vue:我的网站上不断出现'无法加载资源:服务器响应状态为 500(内部服务器错误)”
P粉450079266
P粉450079266 2024-02-21 12:52:46
0
1
451

我正在使用 Vue 和 Django 来做这个项目,但是当我运行我的代码时,我不断收到此错误

"Failed to load resource: the server responded with a status of 500 (Internal Server Error)

127.0.0.1:8000/api/v1/products/winter/yellow-jacket-with-no-zipper:1"

我不断重新加载并等待了 30 分钟这个错误才消失,但它不断出现。 我不知道我的javascript是否有问题,因为我运行vue项目时没有任何错误。

这是我认为有问题的代码。

后端:

产品包中的urls.py模块:

from django.urls import path, include

from product import views

urlpatterns = [
  path('latest-products/', views.LatestProductsList.as_view()),
  path('products/<slug:category_slug>/<slug:product_slug>', views.ProductDetail.as_view()),
]

前端:

Product.vue脚本:

<template>
  <div class="page-product">
    <div class="columns is-multiline">
      <div class="column is-9">
        <figure class="image mb-6">
          <img v-bind:src="product.get_image">
        </figure>

        <h1 class="title">{{ product.name }}</h1>

        <p>{{ product.description }}</p>
      </div>

      <div class="column is-3">
        <h2 class="subtitle">Information</h2>

        <p>Price: <strong>{{ product.price }}</strong></p>

        <div class="field has-addons mt-6">
          <div class="control">
            <input type="number" class="input" min="1" v-model="quantity">
          </div>

          <div class="control">
            <a class="button is-dark">Add to Carts</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'Product',
  data() {
    return {
      product: {},
      quantity: 1
    }
  },
  mounted() {
    this.getProduct()
  },
  methods: {
    getProduct() {
      const category_slug = this.$route.params.category_slug
      const product_slug = this.$route.params.product_slug

      axios
        .get(`/api/v1/products/${category_slug}/${product_slug}`)
        .then(response => {
          this.product = response.data
        })
        .catch(error => {
          console.log("error")
        })
    }
  }
}
</script>

编辑:

经过一番修改,我认为问题是由产品包中的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)

#I think its this line of code
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)

P粉450079266
P粉450079266

全部回复(1)
P粉178132828

修改代码后,我发现我是对的。问题出在产品包中的views.py模块。可以在 ProductDetail 类中的 get_object 函数中看到。

原文:

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

问题是我在定义类别slug时需要添加另一个下划线/下划线(这个东西:_),所以

category_slug=category_slug

变成了

category__slug=category_slug

新版本:

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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板