我无法弄清楚请求变成字符串的位置。我认为两个文件之间的功能之间会有一些错过,但我找不到它。我花了几个小时阅读文档和谷歌搜索,但我找不到问题。
我使用Django Beginner电子商务网站建立了一个网站,我不断收到以下错误:
Traceback (most recent call last):
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/core/handlers/base.py", line 150, in get_response
response = callback(request, **param_dict)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/views/defaults.py", line 21, in page_not_found
return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 140, in render
return self._render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 823, in render
bit = self.render_node(node, context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 837, in render_node
return node.render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/loader_tags.py", line 123, in render
return compiled_parent._render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 823, in render
bit = self.render_node(node, context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 837, in render_node
return node.render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/loader_tags.py", line 155, in render
return self.render_template(self.template, context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/loader_tags.py", line 137, in render_template
output = template.render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 140, in render
return self._render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 823, in render
bit = self.render_node(node, context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 837, in render_node
return node.render(context)
File "/home/craigsander/webapps/abacus_technologies/lib/python2.7/django/template/base.py", line 1178, in render
_dict = func(*resolved_args, **resolved_kwargs)
File "/home/craigsander/webapps/abacus_technologies/abacus/catalog/templatetags/catalog_tags.py", line 10, in cart_box
cart_item_count = cart.cart_distinct_item_count(request)
File "/home/craigsander/webapps/abacus_technologies/abacus/cart/cart.py", line 56, in cart_distinct_item_count
return get_cart_items(request).count()
File "/home/craigsander/webapps/abacus_technologies/abacus/cart/cart.py", line 26, in get_cart_items
return CartItem.objects.filter(cart_id=_cart_id(request))
File "/home/craigsander/webapps/abacus_technologies/abacus/cart/cart.py", line 12, in _cart_id
if request.session.get(CART_ID_SESSION_KEY,'') == '':
AttributeError: 'str' object has no attribute 'session'
catalog_tags.py
from django import template
from cart import cart
from catalog.models import Category, Brand, Product
from blog.models import Article
register = template.Library()
@register.inclusion_tag("tags/cart_box.html")
def cart_box(request):
cart_item_count = cart.cart_distinct_item_count(request)
if cart_item_count > 0:
cart_bool = True
else:
cart_bool = False
return {'cart_item_count': cart_item_count, 'cart_bool': cart_bool }
cart.py
from .models import CartItem
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 CartItem.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('product_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 = CartItem()
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()
def get_single_item(request, item_id):
return get_object_or_404(CartItem, id=item_id, cart_id=_cart_id(request))
# update quantity for single item
def update_cart(request):
postdata = request.POST.copy()
item_id = postdata['item_id']
quantity = postdata['quantity']
cart_item = get_single_item(request, item_id)
if cart_item:
if int(quantity) > 0:
cart_item.quantity = int(quantity)
cart_item.save()
else:
remove_from_cart(request)
# remove a single item from cart
def remove_from_cart(request):
postdata = request.POST.copy()
item_id = postdata['item_id']
cart_item = get_single_item(request, item_id)
if cart_item:
cart_item.delete()
# gets the total cost for the current cart
def cart_subtotal(request):
cart_total = decimal.Decimal('0.00')
cart_products = get_cart_items(request)
for cart_item in cart_products:
cart_total += cart_item.product.price * cart_item.quantity
return cart_total
def is_empty(request):
return cart_distinct_item_count(request) == 0
def empty_cart(request):
user_cart = get_cart_items(request)
user_cart.delete()