简体   繁体   中英

Cannot upload image to MEDIA_ROOT using Mongoengine ImageField

I'm trying to upload images using Mongoengine ImageField. But after uploading a test image,I got page not found by visiting http://127.xxx:xxxx/media/testimage.png .

Raised by: django.views.static.serve "/Users/xxx/Documents/basedir/media/testimage.png" does not exist

myproject/settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

myproject/myapp/models.py

from mongoengine import Document, ImageField

class Image(Document):
        image = ImageField(upload_to="")
        filename = fields.StringField()

myproject/urls.py

from django.conf import settings
urlpatterns = [
    #bla
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

What did I do wrong?

You may want to have a look at this. It is fairly thorough and provides every step needed to configure static files. 404 Error for django serving static files. How to setup django to serve static files? . It appears you are definitely missing STATICFILES_DIRS and also have you ran python manage.py collectstatic command?

It would also be good to confirm you upload_to of the ImageField is also correct. You can verify this in the shell. python manage.py shell

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