Home  >  Q&A  >  body text

Django and Vue: "Unable to load resource: The server responded with a status of 500 (Internal Server Error)" keeps getting on my site

I'm using Vue and Django for this project, but when I run my code I keep getting this error

"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"

I kept reloading and waiting 30 minutes for this error to go away, but it kept coming. I don't know if there is something wrong with my javascript because I run the vue project without any errors.

This is the code that I think is problematic.

rear end:

The urls.py module in the product package:

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()),
]

front end:

Product.vue script:

<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>

edit:

After some modifications, I think the problem is caused by the views.py module in the product package

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粉450079266P粉450079266241 days ago413

reply all(1)I'll reply

  • P粉178132828

    P粉1781328282024-02-22 09:31:57

    After modifying the code, I found that I was right. The problem is with the views.py module in the product package. This can be seen in the get_object function in the ProductDetail class.

    original:

    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

    The problem is that I need to add another underscore/underscore (this thing: _) when defining the category slug, so

    category_slug=category_slug

    became

    category__slug=category_slug

    new version:

    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

    reply
    0
  • Cancelreply