简体   繁体   English

如何更改 Django ImageField 中的默认图像位置?

[英]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.尝试更改 Django 在 ImageField 中查找默认图像的位置时遇到问题。 I am trying to store a default image within a folder in my "media" file.我正在尝试将默认图像存储在我的“媒体”文件的文件夹中。

Code from models.py below:来自models.py的代码如下:

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:加载页面时出现 404 错误:

Not Found: /media/default.jpg [21/Apr/2020 18:11:48] "GET /media/default.jpg HTTP/1.1" 404 1795未找到:/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?关于如何将“profile_pics”片段添加到路径的任何想法?

add media path to your urlpatterns in DEBUG mode在 DEBUG 模式下将媒体路径添加到您的urlpatterns

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您必须在 media/profile_pics 目录中复制带有default.jpg的图像,并将其用作用户的默认图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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