我是python的新手,甚至是DJANGO的新手。我之前写过两个Python应用程序,但是它是自由格式的。我已经开始学习DJANGO Rest Framework了: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/urls.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'))
]
答案 0 :(得分:1)
答案 1 :(得分:1)
我的两分钱:
正如@ 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"
将解决您的问题