如何使用Django管理面板将文件上传到特定地址? [英] how to upload file in a specific address using django admin panel?

查看:159
本文介绍了如何使用Django管理面板将文件上传到特定地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有图像上传功能的django管理面板,图像上传成功完成,但是我无法通过在浏览器中输入网址来访问图像.

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

这是我的代码:

Models.py:

Models.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 .

如何访问url中的图片? 以及如何将文件上传到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管理面板将文件上传到特定地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆