我正在制作api,以获得Android或iphone'用户的坐标,并依赖于该坐标,制作' post_list'(这意味着只是字面上的post_list)。
使用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
之后制作了这些代码但我认为我并没有完全理解上面的文档
请帮助我,我该怎么做才能解决这个问题?
答案 0 :(得分:3)
lat = request.POST.get('user_lat', '13')
lon = request.POST.get('user_lon', '15')
应该是:
lat = self.request.POST.get('user_lat', '13')
lon = self.request.POST.get('user_lon', '15')