Django后台媒体文件显示配置与PyInstaller打包报错解决

闻溪
闻溪
发布时间:2026-10-13 | 2人浏览过
原创

本文详解Django项目中MEDIA_URL与MEDIA_ROOT的配置方法,通过修改全局urls.py实现后台上传媒体文件的正常显示。同时补充了Windows下使用PyInstaller打包Python程序时遇到的TypeError异常及其通过替换bindepend.py文件的解决方案,帮助开发者避开常见部署陷阱。

Django后台媒体文件显示配置

首先,需要在Django项目的全局settings.py文件中进行相关配置。


```
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

接着,在全局的urls.py文件中进行路由设置。

具体代码实现如下: from MxShop.settings import MEDIA_ROOT #导入配置文件中的配置 from django.views.static import serve


urlpatterns = [

 url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),

]

完成上述配置后,后台上传的媒体文件即可正常显示。

PyInstaller打包常见错误解决

补充知识:在PyCharm中使用PyInstaller打包生成.exe文件时,若出现typeerror:expected str,bytes or os.path,not None type错误,可按以下步骤解决。

测试系统环境为Win10 64位。

具体的错误提示信息如下。


Traceback (most recent call last):
 File "C:\Users\user\Desktop\untitled1\venv\Scripts\pyinstaller-script.py", line 11, in <module>
 load_entry_point('PyInstaller==3.4', 'console_scripts', 'pyinstaller')()
 File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\__main__.py", line 111, in run
 run_build(pyi_config, spec_file, **vars(args))
 File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
 PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
 File "C:\Users\user\Desktop\untitled1\venv\lib\site- packages\PyInstaller\building\build_main.py", line 838, in main
 build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build')) 
 File "C:\Users\user\Desktop\untitled1\venv\lib\site- packages\PyInstaller\building\build_main.py", line 784, in build
 exec(text, spec_namespace)
 File "<string>", line 29, in <module> 
 File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\building\api.py", line 424, in __init__
 strip_binaries=self.strip, upx_binaries=self.upx, 
 File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\building\api.py", line 196, in __init__
 self.__postinit__()
 File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
 self.assemble() 
 File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\building\api.py", line 273, in assemble
 pylib_name = os.path.basename(bindepend.get_python_library_path()) 
 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\ntpath.py", line 214, in basename
 return split(p)[1]
 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\ntpath.py", line 183, in split
 p = os.fspath(p) TypeError: expected str, bytes or os.PathLike object, not NoneType

解决该问题的具体方法如下。

访问GitHub页面https://github.com/Loran425/pyinstaller/tree/14b6e65642e4b07a4358bab278019a48dedf7460。

下载页面中的所有文件,解压后找到PyInstaller文件夹下的bindepend.py文件,将其复制并替换到C:\xxx\venv\Lib\site-packages\PyInstaller\depend目录下的bindepend.py文件中。注意,xxx代表你电脑上的实际路径,不同电脑的路径可能有所差异。

随后,在PyCharm的Terminal终端中运行命令:pyinstaller -F xx.py即可。其中xx代表你的Python文件名。

以上关于Django实现后台显示媒体文件的内容就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持本站。

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表