Как я могу получить значение из одной функции в другой функции в Django, views()
Здравствуйте, я новичок в Django. Я хочу получить широту и долготу из функции myview1 в функцию myview, чтобы я мог вывести эти значения и поместить в соответствующий код. PLease Может ли кто-нибудь подсказать мне по этому поводу?
def my_view1(request):
latitude='latitude'
longitude='longitude'
context = {'latitude':latitude, 'longitude':longitude}
my_view(context)
return (context)
@csrf_exempt
@require_http_methods(["POST"])
def my_view(request,context):
if request.method == "POST":
# value_data=(my_view1(data=request.POST))
value_data=my_view1().objects.all()
latitude = request.POST.get(value_data['latitude'])
longitude = request.POST.get(value_data['longitude'])
# In this example I am reading a file with (time, x, y) as dimensions
xarr = xr.open_rasterio('/home/shaheer07/New Rasters/image_factors.tif')
# Slice one of the bands
img = xarr[0, :, :]
#Use the .sel() method to retrieve the value of the nearest cell close to your POI
pixel_value = img.sel(x=latitude, y=longitude, method="nearest")
image = '/home/shaheer07/New Rasters/image_factors.tif'
with rasterio.open(image) as f:
# Load metadata
meta = f.meta
# Use the transform in the metadata and your coordinates
rowcol = rasterio.transform.rowcol(meta['transform'], xs=latitude, ys=longitude, zs=None)
y = rowcol[0]
x = rowcol[1]
# Load specific pixel only using a window
window = Window(x,y,1,1)
raster_values = f.read(window=window)
return JsonResponse(pixel_value,raster_values, status=status.HTTP_201_CREATED)
else:
return JsonResponse('Nothing')
Вы можете использовать сессию Django:
def view_1(request):
request.session['key'] = value
def view_2(request):
value = request.session['key']