django 2.0:不使用正确的视图

时间:2018-05-15 08:12:23

标签: django-2.0

我正在关注基于django 1.8的django书籍项目。它有点过时了,但我尝试将一些部分翻译成django 2.0并且有些成功。

截至目前,该项目有2个应用程序,商店,购物车。商店应用程序很好;当我介绍购物车应用程序时,我现在卡住了好几天,但现在没有经过仔细审查来尝试纠正错误。单击产品项链接时会触发该错误,据推测它应该显示产品详细信息页面,就像它在购物车应用程序之前一样,但现在会生成以下错误:

onload

我无法弄清楚为什么django 坚持使用购物车应用的img.onload = function() { if (img.naturalWidth >= 960 || img.naturalHeight >= 540) { $.ajax({... } else{ $('.cmenu').hide(); console.log('CHOOSEN IMAGE IS TOO SMALL!'); } } 视图而不是商店应用的NoReverseMatch at /1/coke-375ml Reverse for 'cart_add' with arguments '('',)' not found. 1 pattern(s) tried: ['cart\\/add\\/(?P<product_id>[0-9]+)$'] 视图。代码有什么问题?

以下是相关文件。

项目范围内的urls.py

cart_add

店/ urls.py

product_detail

店/ views.py

from django.contrib import admin
from django.urls import path, include

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('cart/', include('cart.urls')),
    path('', include('shop.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

商店/产品/ details.html模板

from django.urls import path
from . import views

app_name = 'shop'
urlpatterns = [
    path('', views.product_list, name='product_list'),
    path('<slug:category_slug>', 
          views.product_list, 
          name='product_list_by_category'),
    path('<int:id>/<slug:slug>',
          views.product_detail,
          name='product_detail'),
 ]

车/ urls.py

from django.shortcuts import render, get_object_or_404
from .models import Category, Product

from cart.forms import CartAddProductForm

def product_list(request, category_slug=None):
    category = None
    categories = Category.objects.all()
    products = Product.objects.filter(available=True)
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        products = products.filter(category=category)
    return render(request,
                'shop/product/list.html',
                {'category': category,
                 'categories': categories,
                 'products': products})

def product_detail(request, id, slug):
    product = get_object_or_404(Product,
                                id=id,
                                slug=slug,
                                available=True)
    cart_product_form = CartAddProductForm()
    return render(request,
                  'shop/product/detail.html',
                  {'product': product,
                   'cart_product_form': cart_product_form})

车/ views.py

{% extends "base.html" %}
{% load static %}

{% block title %}
  {% if category %}{{ category.title }}{% else %}Products{% endif %}
{% endblock %}

{% block content %}
  <div class="product-detail">
    <img src="{% if product.image %}{{ product.image.url }}{% else %}
      {% static "img/no_image.png" %}{% endif %}">
    <h1>{{ product.name }}</h1>
    <h2><a href="{{ product.category.get_absolute_url }}">{{ product.category }}</a></h2>
    <p class="price">${{ product.price }}</p>
    <form action="{% url "cart:cart_add" product_id %}" method="post">
      {{ cart_product_form }}
      {% csrf_token %}
      <input type="submit" value="Add to cart">
    </form>
    {{ product.description|linebreaks }}
  </div>
{% endblock %}

车/ cart.py

from django.urls import path
from cart import views

app_name = 'cart'
urlpatterns = [
    path('', views.cart_detail, name='cart_detail'),
    path('add/<int:product_id>', views.cart_add, name='cart_add'),
    path('remove/<int:product_id>', views.cart_remove, name='cart_remove'),
]

1 个答案:

答案 0 :(得分:0)

shop/product/details.html模板引用product_id中不存在的{% url "cart:cart_add" product_id %}。它应该product.idcart/urls.py中的网址格式匹配。