[英]Can't import name
I am using pyDev for my Django project and I am having a following problem: 我将pyDev用于我的Django项目,并且遇到以下问题:
I always get 'can't import name' error in one of my app's views.py file. 我总是在我的应用程序的views.py文件之一中收到“无法导入名称”错误。
Here is it: 就这个:
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))
The problem is with the very first import. 问题在于第一次导入。
Here is the module I am trying to import : 这是我要导入的模块:
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
Assuming the second chunk of code is cart.py
: 假设第二段代码是
cart.py
:
It looks like you should just do import cart
, as you're specifying the class in your calls: cart.METHOD
. 看起来您应该只
import cart
,因为您在调用中指定了类: cart.METHOD
。
You're getting that error message because there's no class or method inside cart.py
called cart
. 您收到该错误消息是因为
cart.py
没有名为cart
类或方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.