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.
route = get_info(posx, posy, r)line causes the exception. Can you includeget_info()?