简体   繁体   中英

How to change the default image location in the Django ImageField?

Having trouble trying to change where Django looks for the default image in the ImageField. I am trying to store a default image within a folder in my "media" file.

Code from models.py below:

from django.db import models
from django.contrib.auth.models import User

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

When I load the page I get a 404 error:

Not Found: /media/default.jpg [21/Apr/2020 18:11:48] "GET /media/default.jpg HTTP/1.1" 404 1795

Any ideas on how to add the "profile_pics" piece to the path?

add media path to your urlpatterns in DEBUG mode

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

you have to copy a image with default.jpg in media/profile_pics directory and use it for your default image for users

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