在Views.py

时间:2018-07-25 02:27:23

标签: python django

我有一个文件,该文件向API发送请求并检索信息。我们将此文件称为get_info.py。我现在正在构建一个使用Django和views.py文件以及方法“ GET”和“ POST”的GUI。

我现在将功能从get_info.py导入到views.py中,并按以下方式使用

from get_info import get_info
@api_view(['GET'])
def generate_route(request):
    """
    :param request:
    1. lat: posx
    2. lng: pos,
    3. r: radius in km
    4. strategy,
    5. edge_num,
    6. deep,
    :return:
    """
    posx = request.query_params.get('lat', None)
    posy= request.query_params.get('lng', None)

    r= request.query_params.get('r', None)
    strategy = request.query_params.get('strategy', None)
    strategy = strategy if strategy else 3
    edge_num = request.query_params.get('edge_num', None)
    edge_num = edge_num if edge_num else 3
    deep = request.query_params.get('deep', None)
    deep = deep if deep else 3
    print("BEFORE", posx, posy, r, strategy, edge_num, deep)
    route = get_info(posx, posy, r)
    print("AFTER", route)
    if request.query_params.get('lat', None) is not None \
            and request.query_params.get('lng', None) is not None \
            and request.query_params.get('r', None) is not None:
        return Response({}, status=status.HTTP_200_OK)
    else:
        return Response({
            "Error": 'Need lat, lng, and r {}'.format(request.query_params.get('lat', None))
        }, status=status.HTTP_400_BAD_REQUEST)
```

但是,我得到了答复

> (u'BEFORE', u'112.34', u'14.55', u'300.3', 3, 3, 3)
  Internal Server Error: /app/api/v1/get_info/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/decorators.py", line 53, in handler
    return func(*args, **kwargs)
  File "/home/user/Projects/app/views.py", line 750, in generate_route
    route = get_info(posx, posy, r)
  File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 466, in dispatch
    request = self.initialize_request(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 370, in initialize_request
    parser_context=parser_context
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/request.py", line 159, in __init__
    .format(request.__class__.__module__, request.__class__.__name__)
AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `__builtin__.unicode`.

但是当我使用from django.http import HttpRequest建立请求时,它告诉我“超出最大深度”。

get_info方法很长,但简而言之是这样的:

def get_info(posx, posy, r, strategy=3, edge_num=0.8, deep=0):  
    req_url = "http://api.map.baidu.com/direction/v2/driving?origin=posx..."
    trip = requests.get(req_url).json()
    return trip

当我在python shell中运行此get_info方法时,它将返回所需的行程。

1 个答案:

答案 0 :(得分:0)

如果仔细观察一下,剩下的框架就是引起问题的框架;如果getinfo是APIView,则它可能需要请求作为其第一个参数而不是posx(它是字符串)。