无法导入名称

时间:2014-10-29 21:22:06

标签: python django import

我正在为我的Django项目使用pyDev,我遇到了以下问题:

我总是在我的某个应用的views.py文件中收到“无法导入名称”错误。

就是这样:

from cart import cart
from django.template.context import RequestContext
from django.shortcuts import render_to_response


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))

问题在于第一次导入。

以下是我要导入的模块:

from catalog.models import Product 
from models import CartItem
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
import decimal
import random


CART_ID_SESSION_KEY = 'cart_id'

def _cart_id(request):
    if request.session.get(CART_ID_SESSION_KEY,'')=='':
        request.session[CART_ID_SESSION_KEY] = _generate_cart_id()
    return request.sessionp[CART_ID_SESSION_KEY]

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


def get_cart_items(request):
    return CartItem.objects.filter(cart_id=_cart_id(request))

def add_to_cart(request):
    postdata = request.POST.copy()
    product_slug = postdata.get('product_slug','')
    quantity = postdata.get('quantity',1)
    p = get_object_or_404(Product,slug = product_slug)
    cart_products = get_cart_items(request)
    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()



def  get_single_item(request, item_id):
    return get_object_or_404(CartItem,id=item_id,cart_id=_cart_id(request)) 


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) 

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()

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                    

1 个答案:

答案 0 :(得分:1)

假设第二块代码是cart.py

看起来您应该import cart,因为您在通话中指定了该类:cart.METHOD

您收到该错误消息是因为cart.py内没有名为cart的类或方法。