-1

我有一个用 Notepad++ 开发的 Django 程序,但现在我已经为 Eclipse 安装了 Pydev,所以,我想在 Eclipse 中开发这个程序。

我在购物车文件夹下有 cart.py 和 views.py 模块,但是当我在 views.py 文件中导入购物车模块时,我收到一个错误--Unresolved import:cart --- on the views.py 文件导入语句..任何帮助,将不胜感激。

购物车.py

from cart.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 users cart id,set new one if blank
def _cart_id(request):
    print 'Get Cart Id'
    if request.session.get(CART_ID_SESSION_KEY,'') == '':
        print 'Failing here start'
        request.session[CART_ID_SESSION_KEY] = _generate_cart_id()
        print 'Failing here End'

    return request.session[CART_ID_SESSION_KEY]
#
def _generate_cart_id():
    print '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 form the current users 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 producsts in cart
    cart_products = get_cart_items(request)
    #check to see if the item is already in cart
    product_in_cart = False
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
            cart_item.augment_quantity(quantity)
            product_in_cart = True
    if not product_in_cart:
        ci = CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()
def cart_distinct_item_count(request):
    return get_cart_items(request).count()

视图.py

import decimal
from django.shortcuts import render_to_response
from django.template import RequestContext
from ..cart import cart
from django.shortcuts import get_object_or_404

def show_cart(request, template_name="cart/cart.html"): 
     if request.method == 'POST': 
          postdata = request.POST.copy() 
          if postdata['submit'] == 'Remove': 
               cart.remove_from_cart(request) 
          if postdata['submit'] == 'Update': 
               cart.update_cart(request) 
     cart_items = cart.get_cart_items(request) 
     page_title = 'Shopping Cart'    
     cart_subtotal = cart.cart_subtotal(request) 
     return render_to_response(template_name, locals(),
          context_instance=RequestContext(request)) 

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 
4

1 回答 1

0

..前面的可能是在from ..cart import cart惹你。这意味着cart.py应该比views.py从您的问题得出的目录级别高一级 - 并非如此。

于 2012-12-28T00:58:20.060 回答