Django图像上传 - 上传不起作用 [英] Django Image Uploading - Uploads do not work

查看:138
本文介绍了Django图像上传 - 上传不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个Django应用程序,我遇到一个错误,我不确定如何解决。我在网上看过,但仍然没有找到适合我的代码的答案。我试图允许用户上传个人资料图片,但我不知道为什么它不工作。这些文件不会上传到正确的文件夹,如果他们上传的地方我不知道在哪里。我认为这可能与在我的模型中的settings.py和upload_to中引用的路径有关。所有的帮助是赞赏。
提前感谢。 :)

I am currently working on a Django app and I have come across an error that I am unsure how to fix. I have looked about online but still haven't found an answer that suits my code. I am trying to allow users to upload a profile picture however I am not sure why it won't work. The files do not upload to the correct folder and IF they do upload somewhere I am not sure where. I think it may be something to do with the path that is referenced in settings.py and the upload_to in my model. All help is appreciated. Thanks in advance. :)

目录设置映像的文件

settings.py文件(底部)

settings.py file (bottom of it)

STATIC_URL = '/static/'

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

LOGIN_REDIRECT_URL = 'main:home'

AUTH_USER_MODEL = 'main.Designer'

INTERNAL_IPS = '127.0.0.1'

models.py(Designer Model)

models.py (Designer Model)

class Designer(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=25, unique=True)
    display_name = models.CharField(max_length=25)
    twitter = models.CharField(max_length=15)
    bio = models.TextField(max_length=145, blank=True, default="")
    avatar = models.ImageField(upload_to='img', default='img/default_profile_pic.jpg')
    up_votes = models.IntegerField(default=0)
    available = models.BooleanField(default=False)
    thumbnail_price = models.FloatField(null=True)
    channel_art_price = models.FloatField(null=True)
    monthly = models.BooleanField(default=False)
    promoted = models.BooleanField(default=False)
    date_joined = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

如果有其他任何部分需要看到,以帮助解决我的问题,请让我知道。

If there are any other parts you need to see to help solve my problem, please let me know. All help is appreciated.

推荐答案

您需要添加 enctype =multipart / form-data 到您的< form> 元素在模板中,否则浏览器无法将文件发送到服务器。

You need to add enctype="multipart/form-data" to your <form> element in the template, otherwise the browser can't send files to the server.

这篇关于Django图像上传 - 上传不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆