Reputation: 1627
using python 3.4, django 1.9.7 ,django_ajax 0.2.0 ; and test in python 2.7 too ; Here is my code :
from django_ajax.decorators import ajax
from models import Product
from cart.cart import Cart
@ajax
def ajax_add_to_cart(request):
if 'product_id' in request.GET and request.GET['product_id']:
product_id = request.GET['product_id']
product = Product.objects.get(id=product_id)
cart = Cart(request)
cart.add(product, product.price, quantity=1)
items_in_cart = cart.itemCount()
return {'items_in_cart': items_in_cart}
I get this error :
from django_ajax.decorators import ajax
ImportError: cannot import name 'ajax'
Tnx for help
Upvotes: 3
Views: 1170
Reputation: 78554
You've apparently installed the wrong package due to name similarity with another package. That usually happens.
You've installed django_ajax 0.2.0
while you intend to use djangoajax
which has the ajax
decorator. The former does not have or use decorators.
Remove django_ajax
with:
pip uninstall django_ajax
Then install djangoajax
which is usually imported as django_ajax
and would be added as django_ajax
to INSTALLED_APPS
:
pip install djangoajax
Your import would no longer raise an error:
>>> from django_ajax.decorators import ajax
>>>
Upvotes: 3