如何在Django view helper函数中读取shapefile?

如何在Django view helper函数中读取shapefile?,django,Django,作为视图逻辑的一部分,我需要检查纬度和经度点是否在城市边界内。 为此,我将城市形状文件与geopandas一起使用。在纯python代码中,所有这些都可以在本地正常工作。 但是,当我在Django中运行以下代码时: LA_geo_df = gpd.read_file('./City_Boundaries/City_Boundaries.shp') 我得到一个错误: DriverError at / ./City_Boundaries/City_Boundaries.shp: No such f

作为视图逻辑的一部分,我需要检查纬度和经度点是否在城市边界内。 为此,我将城市形状文件与geopandas一起使用。在纯python代码中,所有这些都可以在本地正常工作。 但是,当我在Django中运行以下代码时:

LA_geo_df = gpd.read_file('./City_Boundaries/City_Boundaries.shp')
我得到一个错误:

DriverError at /
./City_Boundaries/City_Boundaries.shp: No such file or directory

加载此类文件的正确Django方式是什么?

自动生成的Django设置文件将具有如下设置

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
这是Django应用程序根目录的绝对文件路径。您可以使用此设置构建文件的绝对路径

CITY_BOUNDARIES_FILE_PATH = os.path.join(BASE_DIR, 'City_Boundaries', 'City_Boundaries.shp')
然后,在要加载文件的位置可以使用该设置

from django.conf import settings

LA_geo_df = gpd.read_file(settings.CITY_BOUNDARIES_FILE_PATH)