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

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

本文详解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 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。