导入不起作用

时间:2014-10-26 06:49:30

标签: python django django-views

我有一个奇怪的问题。我的django项目中有两个应用程序。在一个应用程序中,在views.py中进行的导入之一很好,但在另一个应用程序中,相同的导入显示错误。

这里是views.py,其中导入正常:

from django.shortcuts import get_object_or_404, render_to_response
from models import Category,Product
from django.template.context import RequestContext
from django.core import urlresolvers
from cart import cart
from catalog.forms import ProductAddToCartForm
from django.http.response import HttpResponseRedirect
# Create your views here.

def index(request, template_name="catalog/index.html"):

    page_title = 'Extremely interesting and cheap goods'

    return render_to_response(template_name, locals(),
                              context_instance=RequestContext(request))



def show_category(request,category_slug,template_name='catalog/category.html'):
    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

    return render_to_response(template_name,locals(),
                              context_instance=RequestContext(request))  



def show_product(request, product_slug,template_name="catalog/product.html"):
    p = get_object_or_404(Product,slug=product_slug) 
    categories = p.categories.all()
    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_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        form = ProductAddToCartForm(request = request,label_suffix=':')
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    request.session.set_test_cookie()
    return render_to_response("catalog/product.html",locals(),
                              context_instance=RequestContext(request))    

这里是views.py,导入不起作用:

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

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

我所说的导入是:来自购物车进口购物车。第二个views.py在我尝试导入的cart.py的同一个django应用程序中

2 个答案:

答案 0 :(得分:0)

您需要以这种方式在第二个应用中导入第一个应用的模块

比方说,你的cart.py在目录应用程序中。

# in catalog views
from cart import cart

# in cart app
from catalog.cart import cart

,反之亦然,具体取决于您的cart.py

假设应用已在INSTALLED_APPS中注册

..
'catalog',
'cart',
...

答案 1 :(得分:0)

  1. 将cart.py重命名为carts.py
  2. 删除cart.pyc
  3. 更改您的代码并导入到“购物车”而不是“购物车” 需要
  4. 例如:

    from cart import carts 
    carts.add_to_cart(request)