1
我正在製作獲取'android或iphone'用戶座標的api,並依賴於該座標,生成'post_list'(它意味着僅僅是post_list)。在DjangoRestFramework(DRF)中,沒有定義名稱'request'
使用DRF我做了我的查詢集(這個查詢集就是製作 'post_list')這樣
views.py
from django.shortcuts import render
from django.contrib.gis.geos import GEOSGeometry
from django.contrib.gis.measure import D
from .models import Post
from django.http import HttpResponse
from blog.models import Post, Comment
from blog.serializers import PostSerializer, CommentSerializer
import django_filters
from rest_framework import filters, viewsets, generics
from rest_framework.decorators import permission_classes
from rest_framework.permissions import IsAuthenticated
class PostViewSet(viewsets.ModelViewSet):
serializer_class = PostSerializer
permission_classes = [IsAuthenticated]
def get_queryset(self) :
#these are for getting user's coordinates.
lat = request.POST.get('user_lat', '13')
lon = request.POST.get('user_lon', '15')
userpoint = GEOSGeometry('POINT(' + lat + ' ' + lon + ')', srid=4326)
#these is for making my post_list
result = []
i = 1
while i<50:
list_i = Post.objects.filter(point__distance_lte = (userpoint, D(km=i)))
result.extend(list_i)
if len(result) > 0:
result = result[:0]
break
i += 1
return self.result
錯誤回溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/posts/
Django Version: 1.9.7
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'django.contrib.sites',
'debug_toolbar',
'blog',
'rest_framework',
'rest_framework.authtoken',]
Installed Middleware:
['debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'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']
Traceback:
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/viewsets.py" in view
87. return self.dispatch(request, *args, **kwargs)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
466. response = self.handle_exception(exc)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
463. response = handler(request, *args, **kwargs)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/mixins.py" in list
40. queryset = self.filter_queryset(self.get_queryset())
File "/home/keepair/djangogirls/blog/views.py" in get_queryset
28. lat = request.POST.get('user_lat', '13')
Exception Type: NameError at /posts/
Exception Value: name 'request' is not defined
我使這些代碼如下http://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/#tutorial-6-viewsets-routers,http://www.django-rest-framework.org/api-guide/viewsets/#api-reference
但我覺得我沒在上面
請幫我充分理解的文檔,我應該做些什麼來解決這個問題?
謝謝。它完美的作品! – touchingtwist
不錯。你可以通過接受答案btw來感謝我。 – RickyA