为什么我的异常会给出语法错误

时间:2018-11-14 16:59:51

标签: django django-views

django和python的新功能是我正在建立的网站是一个电子商务网站,它有一个购物车应用程序,该程序可以在views.py文件中处理购物车,我具有此功能:

  def get(self, request):
    try:
        item_id = request.GET.get('item')
        quantity = request.GET.get('qty', 1)
        delete = request.GET.get('delete', 'n')
        cart, is_deleted, cart_item = self._process_cart(item_id, int(quantity), delete, request)
        cart_count = cart.total_count
        request.session['cart_count'] = cart_count
        return Response({'success': True,
         'deleted': is_deleted,
          'count': cart.count,
          'item_total': cart_item.item_total,
          'cart_price': cart.cart_price,
          'cart_count': cart_count}, status=status.HTTP_200_OK)
    except Exception as error:
        print error

它总是给出无效的语法错误

 Traceback (most recent call last):
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\utils\autoreload.py", line 225, in wrapper
 fn(*args, **kwargs)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\core\management\commands\runserver.py", line 117, in 
 inner_run
 self.check(display_num_errors=True)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\core\management\base.py", line 379, in check
 include_deployment_checks=include_deployment_checks,
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\core\management\base.py", line 366, in _run_checks
 return checks.run_checks(**kwargs)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\core\checks\registry.py", line 71, in run_checks
 new_errors = check(app_configs=app_configs)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\core\checks\urls.py", line 13, in check_url_config
 return check_resolver(resolver)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\core\checks\urls.py", line 23, in check_resolver
 return check_method()
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\urls\resolvers.py", line 396, in check
 for pattern in self.url_patterns:
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\utils\functional.py", line 37, in __get__
 res = instance.__dict__[self.name] = self.func(instance)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\urls\resolvers.py", line 533, in url_patterns
 patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\utils\functional.py", line 37, in __get__
 res = instance.__dict__[self.name] = self.func(instance)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\urls\resolvers.py", line 526, in urlconf_module
 return import_module(self.urlconf_name)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37- 
 32\lib\importlib\__init__.py", line 127, in import_module
 return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
 File "<frozen importlib._bootstrap>", line 983, in _find_and_load
 File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 728, in exec_module
 File "<frozen importlib._bootstrap>", line 219, in 
 _call_with_frames_removed
 File "C:\ecommerce-django-master\src\ecommerce\urls.py", line 15, in 
 <module>
 url(r'^cart/', include('carts.urls')),
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
 packages\django\urls\conf.py", line 34, in include
 urlconf_module = import_module(urlconf_module)
 File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37- 
 32\lib\importlib\__init__.py", line 127, in import_module
 return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
 File "<frozen importlib._bootstrap>", line 983, in _find_and_load
 File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 728, in exec_module
 File "<frozen importlib._bootstrap>", line 219, in 
 _call_with_frames_removed
 File "C:\ecommerce-django-master\src\carts\urls.py", line 4, in <module>
 from . import views
 File "C:\ecommerce-django-master\src\carts\views.py", line 56
return Response({'success': True, 'deleted': is_deleted, 'count': 
cart.count, 'item_total': cart_item.item_total, 'cart_price': 
cart.cart_price, 'cart_count':cart_count}, status = status.HTTP_200_OK)
     ^
SyntaxError: invalid syntax

所以我尝试使用if而不是try,但是结果是我的购物车之后无法正常工作,它从未更新过徽章,甚至在下单后也没有任何东西

0 个答案:

没有答案