首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DJango REST框架SyntaxError入门:无效语法

DJango REST框架SyntaxError入门:无效语法
EN

Stack Overflow用户
提问于 2018-11-26 22:01:57
回答 2查看 888关注 0票数 0

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

准确地复制我为测试我们的应用程序接口所做的工作,并运行提供给我:

代码语言:javascript
复制
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

这是我的错误还是系统错误?根据堆栈跟踪,它只命中/tutorial/tutorial/urls.py中的代码

代码语言:javascript
复制
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'))
]
EN

回答 2

Stack Overflow用户

发布于 2018-11-26 22:09:39

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

请考虑降级:

代码语言:javascript
复制
pip install "django-filter<2.0"
票数 1
EN

Stack Overflow用户

发布于 2018-11-26 23:09:58

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

您得到的错误位于第184行:

代码语言:javascript
复制
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

所以:

代码语言:javascript
复制
pip install "django-filter<2.0"

会解决你的问题

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53482772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档