how to upload file in a specific address using django admin panel?

社会主义新天地 提交于 2019-12-20 05:59:21

问题


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

I get this error :

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/media/485508.jpg

Here Is My Codes :

Models.py :

picurl = models.ImageField()

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 .

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 .


回答1:


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)

settings

MEDIA_URL = '/media/'


来源:https://stackoverflow.com/questions/54684872/how-to-upload-file-in-a-specific-address-using-django-admin-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!