所以我试图使用Django Rest Framework。它将作为Ionic混合应用程序的REST API。我需要能够使用街道名称参数(基于用户GPS位置最近的街道)从应用程序发送请求,该请求返回包含该街道名称的数据。我试图按照文档中的Filtering against the URL指南进行操作。
我的代码如下所示:
**urls.py**
router = DefaultRouter()
router.register(r'notes', views.NoteView, base_name='notes')
urlpatterns = patterns('',
url(r'^api/', include(router.urls)),namespace='rest_framework')),
url('^notes/(?P<location>.+)/$', views.NoteView.as_view()),
)
**serializers.py**
class NoteSerializer(serializers.ModelSerializer):
place = serializers.StringRelatedField()
class Meta:
model = Note
fields = ('text_content', 'from_date', 'place')
**views.py**
class NoteView(generics.ListAPIView):
serializer_class = NoteSerializer
def get_queryset(self):
location = self.kwargs['location']
wreturn Note.objects.filter(place__name__contains=location)
我在正则表达式方面不强,但我想我需要在localhost / api / notes / streetname /中查询api以获取该街道名称的数据?
我收到的错误是: / api / notes / Vestergade中的TypeError / as_view()只需1个参数(给定3个)
堆栈跟踪:
Environment:
Request Method: GET
Request URL: http://localhost:8000/api/notes/Vestergade/
Django Version: 1.8
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/Users/andersravn/Projekter/laverapi/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
119. resolver_match = resolver.resolve(request.path_info)
File "/Users/andersravn/Projekter/laverapi/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
366. for pattern in self.url_patterns:
File "/Users/andersravn/Projekter/laverapi/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
402. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/andersravn/Projekter/laverapi/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
396. self._urlconf_module = import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/Users/andersravn/Projekter/laverapi/citystoriesapi/citystoriesapi/urls.py" in <module>
17. url(r'^api/', include(router.urls)),
File "/Users/andersravn/Projekter/laverapi/lib/python2.7/site-packages/rest_framework/routers.py" in urls
79. self._urls = patterns('', *self.get_urls())
File "/Users/andersravn/Projekter/laverapi/lib/python2.7/site-packages/rest_framework/routers.py" in get_urls
315. default_urls = super(DefaultRouter, self).get_urls()
File "/Users/andersravn/Projekter/laverapi/lib/python2.7/site-packages/rest_framework/routers.py" in get_urls
256. view = viewset.as_view(mapping, **route.initkwargs)
Exception Type: TypeError at /api/notes/Vestergade/
Exception Value: as_view() takes exactly 1 argument (3 given)
我似乎无法弄清楚这三个论点的位置。
答案 0 :(得分:7)
偶然发现这个尝试将我的API请求限制为当前用户。
这是关于视图的类型:viewsets.ModelViewSet
。它不应该是View,而是ViewSet - 请检查此问题here
将其更改为(\n[0-9]{3})[ 0-9]{4}([^\n]*)
或类似内容。
答案 1 :(得分:-1)
我注意到的两件事我认为可以帮助您解决问题。
router.register(r'notes', views.NoteView, base_name='notes')
/api
中添加url('^notes/(?P<location>.+)/$', views.NoteView.as_view()),
或只访问/notes/*LOCATION*