我正在嘗試將項(xiàng)目文件保存到媒體,然后在 url 中顯示,到目前為止,我已經(jīng)遵循了一些簡單的技術(shù)來避免文件操作并將它們存儲在媒體中我的setting.py文件->STATIC_ROOT = os.path.join(BASE_DIR, 'static')STATIC_URL = '/static/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media')MEDIA_URL = '/media/'我的 models.py 文件模型字段->picture = models.ImageField(null=True, blank=True, upload_to = "photos")resume = models.FileField(null=True, blank=True, upload_to = "resume")然后它會正確存儲并顯示為媒體網(wǎng)址。我的 urls.py 文件->from django.contrib import adminfrom django.urls import path,includefrom django.conf.urls.static import staticfrom django.conf import settingsurlpatterns = [ path('admin/', admin.site.urls), path('api/v1/cand/',include("core.urls"))] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)主要問題發(fā)生在我查詢簡歷 pdf 文件時。它找不到 BASE_DIRfrom django.conf import BASE_DIRurl = serializer.data["resume"]file_name = url.split("/")[-1]data=ResumeParser(os.path.join(BASE_DIR,"/media/resume/",file_name)).get_extracted_data()return Response({**serializer.data, "data": data}, status = status.HTTP_200_OK)雖然當(dāng)我使用絕對路徑時它有效-> url = serializer.data["resume"] file_name = url.split("/")[-1] data=ResumeParser("/home/riyad/Desktop/kalkedev/services/candidate","media/resume/",file_name).get_extracted_data() return Response({**serializer.data, "data": data}, status = status.HTTP_200_好的)這是一個 django 問題嗎,因?yàn)槲抑耙恢痹谔幚磉@個 issur 并采取簡單的方法(默認(rèn) base_dir)來存儲文件。需要快速找到答案。
可疑文件操作:無法在我的 django 項(xiàng)目文件夾中配置媒體根目錄和基本目錄
人到中年有點(diǎn)甜
2023-08-08 10:29:51