带有两个应用程序视图的项目中的Django图像上传未上传文件

时间:2020-04-02 17:15:04

标签: python django django-models django-views django-templates

我正在尝试使用两个应用程序 home main 在Django中上传图像,我的项目名称为register。只是为了测试是否正确添加了媒体根目录,我正在使用以下方法在html模板中显示图像:

<img src="{{ MEDIA_URL }}profile_image/profile_default.png " alt="HAHA">

但是,我收到404错误消息:

Not Found: /home/profile_image/profile_default.png
[02/Apr/2020 16:58:50] "GET /home/profile_image/profile_default.png HTTP/1.1" 404 8402

我的设置中已经包含了这个

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

我的项目网址中也有此内容:

urlpatterns = [

 #my urls

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我正在使用django_uuid_upload软件包。

供参考,我的文件夹是

    register
     -home
     -main
     -register
     --settings.py
     --urls.py
     -media
     --post_images
     --profile_image
     ---profile_default.png
     -static

感谢所有的帮助!

1 个答案:

答案 0 :(得分:1)

404错误Not Found: /home/profile_image/profile_default.png显示该URL根本没有引用您的媒体文件夹,因此该路径不会转到您的/media/profile_image/profile_default.png

uuid-upload库将文件夹路径设置为upload_to_uuid('post_images', make_dir=True,这将其设置为目录dir/profile_image/

将该路径设置为您的媒体路径,然后将profile_image路径设置为这样

image = models.FileField(upload_to=upload_to_uuid('media/post_images', make_dir=True),verbose_name='Image')

以便与您的媒体文件夹配合使用。

相关问题