简体   繁体   English

ImageField 未在 Django 中上传

[英]ImageField is not uploaded in Django

I'm struggling with uploding an image using Django.我正在努力使用 Django 上传图像。 Here is my model, where i have the image field:这是我的 model,其中我有图像字段:

class User(AbstractBaseUser, PermissionsMixin):
    USERNAME_FIELD = 'email'  
        ...
    photo = models.ImageField(upload_to="images/", default=None, blank=True, null=True)
  

I've read another posts on Stack overflow, and i found that in settings.py i have to define MEDIA_ROOT and MEDIA_URL .我已经阅读了有关堆栈溢出的另一篇文章,我发现在settings.py我必须定义MEDIA_ROOTMEDIA_URL This is what i have in my file:这是我的文件中的内容:

MEDIA_URL = '/images/'
MEDIA_ROOT = '/home/anamaria/workspace/AllFest2/festivals/user/images/'

And now, in url.py i need to define my urlpatterns:现在,在 url.py 我需要定义我的 urlpatterns:

urlpatterns = [
   .
   .
   .

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

I believe that the path is not given the right way.我相信没有给出正确的道路。 But i don't know why.但我不知道为什么。 How should i do it?( the user folder is in 'AllFest2/festival', which is my project's root.我该怎么做?(用户文件夹位于“AllFest2/festival”中,这是我项目的根目录。

 user
    ├── admin.py
    ├── api
    │   ├── __init__.py
    │   ├── permissions.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-36.pyc
    │   │   ├── permissions.cpython-36.pyc
    │   │   ├── serializers.cpython-36.pyc
    │   │   └── views.cpython-36.pyc
    │   ├── serializers.py
    │   └── views.py
    ├── images
    ├── __init__.py
    ├── manager.py
    ├── migrations
    │   ├── 0001_initial.py
    │   ├── __init__.py
    │   └── __pycache__
    │       ├── 0001_initial.cpython-36.pyc
    │       └── __init__.cpython-36.pyc
    ├── models.py
    ├── OCR.py

I assume that you also defined a base DIR in your settings, which results in a wrong MEDIA_ROOT :我假设您还在设置中定义了一个基本 DIR,这会导致错误的MEDIA_ROOT

settings.py设置.py

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

Please not that it is common practice to store the media files in a own directory media which would result in the following config:请注意,将媒体文件存储在自己的目录media中是常见的做法,这将导致以下配置:

settings.py设置.py

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

urls.py网址.py

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

Also is user an app or your project dir? user也是应用程序还是您的项目目录? I guess an app, since it doesn't contain settings.py .我猜是一个应用程序,因为它不包含settings.py Don't store your static and media files on app level, this is bad practice.不要在应用程序级别存储 static 和媒体文件,这是不好的做法。

I think you should add enctype in template form我认为您应该以模板形式添加 enctype

<form action="action_url" method="post" enctype="multipart/form-data"

and also while getting you have to do as mentioned below in views.py并且同时让你必须按照下面在views.py中提到的那样做

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)

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

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