0

I have a file that sends a request to an API and retrieves information. Let's call this file get_info.py. I am now building a GUI that uses Django and the views.py file, with methods 'GET' and 'POST'.

I am now importing the function from get_info.py into views.py and using it as follows

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)
```

However, I get the response

> (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`.

But when I use from django.http import HttpRequest to build my request, it tells me 'maximum depth exceeded'.

The get_info method is quite long, but in a nutshell it looks like this:

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

When I run this get_info method in my python shell, it returns the desired trip.

2
  • route = get_info(posx, posy, r) line causes the exception. Can you include get_info() ? Commented Jul 25, 2018 at 3:35
  • Included it. It's very long but that should show you the gist of it. Commented Jul 25, 2018 at 8:20

1 Answer 1

1

If you look closely rest framework is the one which is causing the problem, If the getinfo is an APIView then it might need request as its first argument not posx which is a string.

Sign up to request clarification or add additional context in comments.

7 Comments

I tried changing the argument to get_info as get_info(request) and it tells me: AssertionError: The 'request' argument must be an instance of 'django.http.HttpRequest', not 'rest_framework.request.Request.
This is strange, can you show the function signature and decorators if any
function signature and decorators of get_info? well, it doesn't have any decorators (maybe it should?) and the signature is that all the arguments are floats.
try passing request._request to get_info and see if that fixes the issue.
That would mean the private request attr might be the request itself and calling itself over and over again. You can either lookup how to extract django http from rest http. Or change your code to function much smoothly. Calling another view while inside a view doesnt seem like a good pattern to me. You could consider extracting logic to its own classes and using views as tools to deliver response
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.