简体   繁体   中英

Django uploading images via admin panel error

Hello when I want to test uploading images via admin panel I keep getting an error. No such file or directory: '/todoapp/media/cola.png'. I have no idea what did I wrong and unluckily I have to paste ton of code.

model:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=CASCADE)
    image = models.ImageField(upload_to='media/', default='ww1.jpg')

When image goes default I get an error:

No such file or directory: '/todoapp/media/home/static/images/ww1.jpg'

urls.py

urlpatterns =+  static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


tree:

├── docker-compose.yml
├── Dockerfile
├── home
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── models.py
│   ├── signals.py
│   ├── static
│   │   └── home
│   │       ├── etc. etc.
│   ├── templates
│   │   └── home
│   │       ├── etc. etc.
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── media
│   ├── dress.png
│   └── pictures
│       └── ww1.jpg
├── requirements.txt
├── todoapp
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── travis_script.sh

settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urlpatterns =+  static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


tree:

├── docker-compose.yml
├── Dockerfile
├── home
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── models.py
│   ├── signals.py
│   ├── static
│   │   └── home
│   │       ├── etc. etc.
│   ├── templates
│   │   └── home
│   │       ├── etc. etc.
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── media         #chack here
│   └── ww1.jpg  
│   ├── dress.png   
│   └── pictures
├── requirements.txt
├── todoapp
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── travis_script.sh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM