我不能看到是我犯了一个错误,我几乎尝试了一切。即使检查计算器,但我无法找到错误...请你可以检测我在哪里呢?
这是PRINTSCREEN的错误:
店/ views.py:
from django.shortcuts import render, get_list_or_404
from .models import Category, Product
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_list_or_404(Category, slug=category_slug)
products = Product.objects.filter(category=category)
context = {
'category': category,
'categories': categories,
'products': products
}
return render(request, 'shop/list.html', context)
def product_detail(request, id, slug):
product = get_list_or_404(Product, id=id, slug=slug, available=True)
context = {
'product': product
}
render(request, 'shop/detail.html', context)
urls.py在商店应用程序:
from django.urls import path
from . import views
app_name = 'shop'
urlpatterns = [
path('', views.product_list, name='product-list'),
path('<category_slug>/', views.product_list, name='product_list_by-category'),
path('<slug>/', views.product_detail, name='product-detail'),
]
base.html文件在商店的应用程序使用模板,然后再开店文件夹:
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}On-line Shop{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
{% include 'shop/navbar.html' %}
{% block content %}
{% endblock %}
<script src="{% static 'js/jquery.min.js' %}" type="text/javascript"></script>
<script src="{% static 'js/bootstrap.min.js' %}" type="text/javascript"></script>
</body>
</html>
models.朋友 :
from django.db import models
from django.urls import reverse
class Category(models.Model):
name = models.CharField(max_length=150, db_index=True)
slug = models.SlugField(max_length=150, unique=True ,db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name', )
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category', args=[self.slug])
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
name = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(max_length=100, db_index=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
available = models.BooleanField(default=True)
stock = models.PositiveIntegerField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
class Meta:
ordering = ('name', )
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_detail', args=[self.id, self.slug])
如果你有什么事告诉我。但我认为这是足以发现错误。请帮帮忙,IM坚持2天,这个错误,并不能解决它。 <3
我发现了一个错误....在我的list.html我misspeled错误的URL模式。 ({% url 'shopproduct_list' %})
,相反,我只是需要以防万一用我的网址这样{% url 'product-list' %}
的名字,如果U可以在乌拉圭回合的HTML文档此错误检查的网址。也许你拼写错误的东西。
干杯