首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >每次单击add to cart按钮时都会出现错误

每次单击add to cart按钮时都会出现错误
EN

Stack Overflow用户
提问于 2018-07-05 05:07:19
回答 1查看 71关注 0票数 0

我在一个正在建设的电子商务网站中创建了一个购物车应用程序,但当我单击页面上呈现的添加购物车表单时,它没有通过。任何帮助我们都将不胜感激。

在购物车应用程序中,我有普通的django文件。(__init__.pyadmin.pyapps.pycart.py (由我添加),models.pytests.pyurls.pyviews.py)

这是我的购物车应用程序中的文件内容,我用这些内容填充了这些文件。对于cart.py:

代码语言:javascript
复制
from cart.models import CartItems
from catalog.models import Product
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
import decimal
import random

CART_ID_SESSION_KEY = 'cart_id'
# get the current user's cart id, sets new one if blank
def _cart_id(request):
    if request.session.get(CART_ID_SESSION_KEY,'') == '':
        request.session[CART_ID_SESSION_KEY] = _generate_cart_id()
    return request.session[CART_ID_SESSION_KEY]


def _generate_cart_id():
    cart_id = ''
    characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()'
    cart_id_length = 50
    for y in range(cart_id_length):
        cart_id += characters[random.randint(0, len(characters)-1)]
    return cart_id

# return all items from the current user's cart
def get_cart_items(request):
    return CartItems.objects.filter(cart_id=_cart_id(request))


# add an item to the cart
def add_to_cart(request):
    postdata = request.POST.copy()
    # get product slug from post data, return blank if empty
    product_slug = postdata.get('self.slug','')
    # get quantity added, return 1 if empty
    quantity = postdata.get('quantity',1)
    # fetch the product or return a missing page error
    p = get_object_or_404(Product, slug=product_slug)
    #get products in cart
    cart_products = get_cart_items(request)
    product_in_cart = False
    # check to see if item is already in cart
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
        # update the quantity if found
            cart_item.augment_quantity(quantity)
            product_in_cart = True
    if not product_in_cart:
        # create and save a new cart item
        ci = CartItems()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()


# returns the total number of items in the user's cart
def cart_distinct_item_count(request):
    return get_cart_items(request).count()

models.py:   
from django.db import models
from catalog.models import Product

class CartItems(models.Model):
    cart_id = models.CharField(max_length=50)
    date_added = models.DateTimeField(auto_now_add=True)
    quantity = models.IntegerField(default=1)
    product = models.ForeignKey(Product, unique=False, 
on_delete=models.CASCADE)

    class Meta:
        db_table = 'cart_items'
        ordering = ['date_added']

    def total(self):
        return self.quantity * self.product.price

    def name(self):
        return self.product.name

    def price(self):
        return self.product.price

    def get_absolute_url(self):
        return self.product.get_absolute_url

urls.py:

代码语言:javascript
复制
   from django.urls import path
   from cart import views

   urlpatterns = [
       path('', views.show_cart, name='show_cart')
    ]

views.py:

代码语言:javascript
复制
from django.shortcuts import render
from django.template import RequestContext
from cart import cart

def show_cart(request):
    cart_item_count = cart.get_cart_items(request)
    context_instance = RequestContext(request)
    page_title = "showing Cart"
    context = {'page_title' : page_title,
                'cart_item_count': cart_item_count,
                'context_instance': context_instance
                }
    template = 'cart/cart.html'
    return render(request, template, context)

以下是项目中继承了购物车应用程序的另一个应用程序(目录)的models.py、views.py和forms.py

models.py:

代码语言:javascript
复制
    from django.db import models
    from django.urls import reverse

class Category(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    slug = models.SlugField(max_length=50, unique=True, help_text="Unique 
           value for product page URL, created from name.")
    description = models.TextField(blank=False)
    is_active = models.BooleanField(default=True)
    meta_keywords = models.CharField("Meta Keywords", max_length=255, 
         help_text="Comma-delimited set of SEO keywords for meta tag")
    meta_description = models.CharField("Meta Description", max_length=255, 
                    help_text="Content for description meta tag")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "category"
        ordering = ["-created_at"]
        verbose_name_plural = "Categories"

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('catalog:show_category', args=[self.slug])


class Product(models.Model):
    name = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(max_length=255, unique=True, help_text="Unique 
           value for product page URL, created from name.")
    brand = models.CharField(max_length=50)
    sku = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=9, decimal_places=2)
    old_price = models.DecimalField(max_digits=9, decimal_places=2, 
               blank=True, default=0.00)
    image = models.ImageField(upload_to='media', blank=True)
    is_active = models.BooleanField(default=True)
    is_bestseller = models.BooleanField(default=False)
    is_featured = models.BooleanField(default=False)
    quantity = models.IntegerField()
    description = models.TextField(blank=False)
    meta_keywords = models.CharField(max_length=255, help_text = 'Comma- 
              delimited set of SEO keywords for meta tag')
    meta_description = models.CharField(max_length=255, help_text = 'Content 
                     for description meta tag')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    categories = models.ManyToManyField(Category)

    class Meta:
        db_table = 'products'

        ordering = ['-created_at']

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('catalog:show_product', args=[self.slug])

    def sale_price(self):
        if self.old_price > self.price:
            return self.price
        else:
            return None

views.py:

代码语言:javascript
复制
    from django.shortcuts import render, get_object_or_404, redirect
    from catalog.models import Category, Product
    from django.template import RequestContext
    from django.urls import reverse
    from cart import cart
    from django.http import HttpResponseRedirect
    from catalog.forms import ProductAddToCartForm

def index(request):
    context_instance = RequestContext(request)
    template = "catalog/index.html"
    page_title = 'Musical Instruments and Sheet Music for Musicians'
    context = {'page_title': page_title,
           'context_instance': context_instance}
    return render(request, template, context)


def show_category(request, category_slug):
    template = "catalog/category.html"
    context_instance = RequestContext(request)
    c = get_object_or_404(Category, slug=category_slug)
    products = c.product_set.all()
    page_title = c.name
    meta_keywords = c.meta_keywords
    meta_description = c.meta_description
    context = {
        'context_instance': context_instance,
        'c': c,
        'products': products,
        'page_title': page_title,
        'meta_keywords': meta_keywords,
        'meta_description': meta_description,
    }
    return render(request, template, context)


def show_product(request, product_slug):
    p = get_object_or_404(Product, slug=product_slug)
    context_instance = RequestContext(request)
    categories = p.categories.filter(is_active=True)
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    if request.method == 'POST':
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        if form.is_valid():
            cart.add_to_cart(request)
            if request.session.test_cookie_worked():
                request.session.delete_test_cookies()
            return redirect('cart: show_cart')
    else:
        form = ProductAddToCartForm(request=request, label_suffix=':')
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    request.session.set_test_cookie()
    context = {
        'context_instance': context_instance,
        'p': p,
        'categories': categories,
        'page_title': page_title,
        'meta_keywords': meta_keywords,
        'meta_description': meta_description,
        'form':form
    }
    template = "catalog/product.html"
    return render(request, template, context)

urls.py:

代码语言:javascript
复制
    from django.urls import path
    from catalog import views

app_name = 'catalog'
urlpatterns = [
    path('', views.index, name='index'),
    path('<category_slug>/', views.show_category, name='show_category'),
    path('product/<product_slug>', views.show_product, name='show_product'),

]

表单嵌入的模板

代码语言:javascript
复制
    {% extends "catalog.html" %}
    {% block content %}
    {% block sidebar %}
    {% endblock %}
    <div class="container mt-2 pl-5">
<div class="row text-justify ml-5 pl-5">
    <div class="col-md img-responsive">
        <img src="{{ p.image.url }}" alt="{{ p.name }}" class="img-responsive 
center-block img-thumbnail"  width="100%"/>
    </div>
    <div class="col-md">
        <h4>{{ p.name }}</h4>
        Brand: <em>{{ p.brand }}</em>
        <br /><br />
        SKU: {{ p.sku }}
        <br />
        In categor{{ categories.count|pluralize:"y,ies" }}:
            {% for c in categories %}
        <a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
        {% if not forloop.last %}, {% endif %}
            {% endfor %}
            <br /><br />
        {% if p.sale_price %}
        Was: <s>$ {{ p.old_price }}</s>
        <br />
        Now: $ {{ p.price }}
        {% else %}
        Price: $ {{ p.price }}
        {% endif %}
        <br /><br />
<form method="POST" action=".">
    {{ form.as_p }}
     {% csrf_token %}
    <br />
    <input type="submit" value="Add To Cart" name="submit" alt="Add To Cart" 
/>
</form>
    </div>
</div>

<div class=" text-center pt-5 mb-5 pb-5">
    <h3>Product Description</h3>
    {{ p.description }}
</div>
    </div>
 {% endblock %}

每当我单击add to cart时,我都会收到以下错误:

代码语言:javascript
复制
Page not found (404)
    Request Method: POST
    Request URL:http://127.0.0.1:8000/catalog/product/
    Raised by:catalog.views.show_category
EN

回答 1

Stack Overflow用户

发布于 2018-07-05 05:13:38

正如您在错误消息中看到的那样

代码语言:javascript
复制
Raised by:catalog.views.show_category

catalog.views.show_category引发了该错误,因此由于它是404,因此它可能是由

代码语言:javascript
复制
c = get_object_or_404(Category, slug=category_slug)

现在,它正在尝试搜索一个类别...为什么?url为http://127.0.0.1:8000/catalog/product/

让我们检查一下你的路径

代码语言:javascript
复制
 path('<category_slug>/', views.show_category, name='show_category'),

有趣的是,任何跟有斜杠的路径都会调用show_category!这不是您想要的,这意味着http://127.0.0.1:8000/catalog/product/正在尝试显示一个名为'product'!!

现在有多种方法可以修复它。所有这些都涉及到重构路径(并相应地更改应用程序的其余部分)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51180751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档