【问题标题】:Django uploading images via admin panel errorDjango通过管理面板错误上传图片
【发布时间】:2021-12-05 14:06:40
【问题描述】:

您好,当我想通过管理面板测试上传图片时,我不断收到错误消息。没有这样的文件或目录:'/todoapp/media/cola.png'。我不知道我做错了什么,不幸的是我不得不粘贴大量代码。

型号:

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

当图像变为默认时,我得到一个错误:

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/'

【问题讨论】:

  • 请同时显示您的设置,特别是 BASE_DIR、PROJECT_ROOT 和任何 MEDIA 设置。
  • 尝试添加default='pictures/ww1.jpg'
  • 添加了设置!
  • 把你的默认img放在media/ww1.jpg而不是media/pictures/ww1.jpg

标签: python django


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2015-03-18
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多