how to upload file in a specific address using django admin panel?(如何使用 django 管理面板在特定地址上传文件?)
问题描述
我正在开发一个具有图像上传功能的 django 管理面板,图像上传成功,但我无法通过在浏览器中输入 url 访问图像.
I'm Developing a django admin panel that has image upload capability ,image upload works successfully , but i cant access to images from entering urls in browser .
当我想尝试访问这样的图片时:
When I want to try to access to a picture like this :
http://127.0.0.1:8000/media/485508.jpg
我得到这个错误:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/485508.jpg
这是我的代码:
模型.py:
picurl = models.ImageField()
Settings.py:
Settings.py :
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'http://127.0.0.1:8000/media/'
if not os.path.exists(MEDIA_ROOT):
os.makedirs(MEDIA_ROOT)
我可以看到指定文件夹中的文件成功上传了.
and i can see the file in the specified folder successfully uploaded .
如何访问网址中的图片?以及如何将文件上传到 Wamp WWW 文件夹?任何建议都会有所帮助.谢谢.
How can i access to pictures in the url ? and How Can I upload files to Wamp WWW Folder ? any suggestions will be helpfull . thanks .
推荐答案
在您的主 urls.py
中为它们添加模式,如下所示.
In your main urls.py
add pattern for them like below.
urls.py
...
from django.conf import settings
from django.conf.urls.static import static
...
urlpatterns = [
...
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
设置
MEDIA_URL = '/media/'
这篇关于如何使用 django 管理面板在特定地址上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 django 管理面板在特定地址上传文件?
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12