问题
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