代码之家  ›  专栏  ›  技术社区  ›  Devin Dixon

django rest framework语法入门错误:无效语法

  •  0
  • Devin Dixon  · 技术社区  · 6 年前

    我是python新手,对django更是新手。我以前写过两个python应用程序,但都是免费的。我已经开始学习django rest框架: https://www.django-rest-framework.org/tutorial/quickstart/

    准确地复制我已经完成了对我们的api的测试,运行python manage.py runserver给了我:

    Performing system checks...
    
    Unhandled exception in thread started by <function wrapper at 0x10350c410>
    Traceback (most recent call last):
      File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
        fn(*args, **kwargs)
      File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
        self.check(display_num_errors=True)
      File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 359, in check
        include_deployment_checks=include_deployment_checks,
      File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
        return checks.run_checks(**kwargs)
      File "/Library/Python/2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
        new_errors = check(app_configs=app_configs)
      File "/Library/Python/2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
        return check_resolver(resolver)
      File "/Library/Python/2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
        return check_method()
      File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 256, in check
        for pattern in self.url_patterns:
      File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 35, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 407, in url_patterns
        patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
      File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 35, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 400, in urlconf_module
        return import_module(self.urlconf_name)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
      File "/Users/devindixon/Sites/Hearst/tutorial/tutorial/urls.py", line 18, in <module>
        from rest_framework import routers
      File "/Library/Python/2.7/site-packages/rest_framework/routers.py", line 25, in <module>
        from rest_framework import views
      File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 16, in <module>
        from rest_framework import exceptions, status
      File "/Library/Python/2.7/site-packages/rest_framework/exceptions.py", line 17, in <module>
        from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
      File "/Library/Python/2.7/site-packages/rest_framework/utils/serializer_helpers.py", line 8, in <module>
        from rest_framework.compat import unicode_to_repr
      File "/Library/Python/2.7/site-packages/rest_framework/compat.py", line 77, in <module>
        import django_filters
      File "/Library/Python/2.7/site-packages/django_filters/__init__.py", line 4, in <module>
        from .filterset import FilterSet
      File "/Library/Python/2.7/site-packages/django_filters/filterset.py", line 184
        def __init__(self, data=None, queryset=None, *, request=None, prefix=None):
                                                      ^
    SyntaxError: invalid syntax
    

    这是我的错误还是系统错误?根据stacktrace,它只在/tutorial/tutorial/url.py上点击我的代码

    from django.conf.urls import url, include
    from rest_framework import routers <----THIS IS LINE 18
    from tutorial.quickstart import views
    
    router = routers.DefaultRouter()
    router.register(r'users', views.UserViewSet)
    router.register(r'groups', views.GroupViewSet)
    
    # Wire up our API using automatic URL routing.
    # Additionally, we include login URLs for the browsable API.
    urlpatterns = [
        url(r'^', include(router.urls)),
        url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
    ]
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Linovia    6 年前

    您正在使用与最新版本不兼容的Python2.7 django-filter 你能看到的版本 here

    请考虑降级:

    pip install "django-filter<2.0"
    
        2
  •  1
  •   edilio    6 年前

    我的两分钱: 正如@9769953和@livonia所说 django-filter 你在使用 https://github.com/carltongibson/django-filter/blob/master/django_filters/filterset.py#L184 Python 3.x 语法与不兼容 Python 2.7 所以这个版本。新语法指定于 https://www.python.org/dev/peps/pep-3102/ .

    你得到的错误在184行:

    File "/Library/Python/2.7/site-packages/django_filters/filterset.py", line 184
    def __init__(self, data=None, queryset=None, *, request=None, prefix=None):
                                                  ^
    

    SyntaxError: invalid syntax

    所以:

    pip install "django-filter<2.0"
    

    会解决你的问题