我无法在HTML文件中输入数量订单以添加到购物车 - Django

时间:2018-04-15 04:03:07

标签: python django

我在Django博客上工作,并为详细信息页面实施了类别。我偶然发现了一个问题。

我创建了一个 forms.py 文件,以便将商品添加到购物车,这是以下代码。

cart / forms.py 文件

from django import forms


PRODUCT_QUANTITY_CHOICES = [(i, str(i)) for i in range(1, 21)]


class CartAddProductForm(forms.Form):
    quantity = forms.TypedChoiceField(choices=PRODUCT_QUANTITY_CHOICES,
                                      coerce=int)
    update = forms.BooleanField(required=False,
                                initial=False,
                                widget=forms.HiddenInput)

cart / views.py 代码文件

from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
from myshop.models import Product
from .cart import Cart
from .forms import CartAddProductForm


@require_POST
def cart_add(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    form = CartAddProductForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        cart.add(product=product,
                 quantity=cd['quantity'],
                 update_quantity=cd['update'])
    return redirect('cart:cart_detail')


def cart_remove(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    cart.remove(product)
    return redirect('cart:cart_detail')


def cart_detail(request):
    cart = Cart(request)
    for item in cart:
        item['update_quantity_form'] = CartAddProductForm(initial={'quantity': item['quantity'],
                                                                   'update': True})
    return render(request, 'cart/detail.html', {'cart': cart})

我使用以下代码显示在产品页面中创建的表单

<form action="{% url "cart:cart_add" product.id %}" method="post">
    {{ cart_product_form }}
    {% csrf_token %}
    <input type="submit" value="Add to cart">
</form>

这是 myshop / templates / product / detail.html 文件,我把代码放在

{% extends "shop/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>
        {% for category in product.category.all %}
        <h3><a href="{{ category.get_absolute_url }}">{{ category }}</a>{% if not forloop.last %}, {% endif %}</h3>
        {% endfor %}
        <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 %}

这是 myshop / views.py 文件

from django.shortcuts import render, get_object_or_404
from .models import Category, Product
from cart.forms import CartAddProductForm

# Create your views here.

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, product_slug):
    product = get_object_or_404(Product, slug=product_slug, available=True)
    cart_product_form = CartAddProductForm()
    return render(request, 'shop/product/detail.html', {'product': product}, {'cart_product_form': cart_product_form})

我按照示例书中的Django教程。

我错过了什么吗?

修改

我想显示如下图所示的结果 enter image description here

但结果我得到如下图 enter image description here

没有数量输入来执行订单

1 个答案:

答案 0 :(得分:1)

将上下文传递给render的方式是错误的 - 您需要将所有上下文变量传递到单个字典中,但是您已经传递了两个单独的字典:

def product_detail(request, product_slug):
    # ...
    return render(request, 'shop/product/detail.html', 
                  {'product': product}, {'cart_product_form': cart_product_form})
                                      # ^ This is wrong

将其更改为:

def product_detail(request, product_slug):
    # ...
    return render(
        request, 
        'shop/product/detail.html', 
        {'product': product, 'cart_product_form': cart_product_form}
    )