简体   繁体   English

django ImageField没有上传文件

[英]django ImageField not uploading the file

So i've been googling this issue for the past hour and can't come up with a solution. 因此,过去一个小时来我一直在搜索该问题,无法提出解决方案。 Basically this is it: in my model.py i have a class that has this 基本上就是这样:在我的model.py中,我有一个包含此内容的类

class Case(models.Model):
zoomOutImage = models.ImageField('Label', upload_to="zoomOutImage")

and in my settings.py i have my media URL/ROOT set up like this 在我的settings.py中,我的媒体URL / ROOT设置如下

MEDIA_ROOT = os.path.join(os.path.abspath(''),'app/static/ds/')
MEDIA_URL = '/static/ds/'

which from the webserver should serve out like this: 来自网络服务器的内容应如下所示:

http://127.0.0.1:8000/static/ds/zoomOutImage/actinic_granuloma_3.jpg

I've installed PIL (inside virtualENV) and there are no errors in uploading, the only issue is when i try uploading the file via the admin panel nothing happens. 我已经安装了PIL(位于virtualENV内),并且上传没有错误,唯一的问题是,当我尝试通过管理面板上传文件时,没有任何反应。 No errors nothing. 没有错误,没有。 The file just simply doesn't get uploaded to the zoomOutImage folder by the development server. 该文件只是不会被开发服务器上传到zoomOutImage文件夹。 Can anyone point me towards why? 谁能指出我的原因?

I guess your file is in a subdir of your root, subdir named 'zoomOutImage'. 我猜您的文件位于根目录的子目录中,该子目录名为“ zoomOutImage”。 Or even a file called like that in the root. 甚至是根目录中的类似文件。 I remember putting a function call in the upload to string. 我记得在上传到字符串中放置了一个函数调用。 That function creates a path and filename, using os.join and the filename from the instance. 该函数使用os.join和实例中的文件名创建路径和文件名。 Doing this by head, no example code available right now. 手动执行此操作,现在没有示例代码。 But must be able to google this. 但是必须能够用谷歌搜索。

Look here https://stackoverflow.com/questions/1190697/django-filefield-with-upload-to-determined-at-runtime 在这里查看https://stackoverflow.com/questions/1190697/django-filefield-with-upload-to-determined-at-runtime

And by the way, I totally disagree with your answer, you should NEVER use absolute paths in your settings! 顺便说一句,我完全不同意您的回答,您永远不要在设置中使用绝对路径! See this answer use css in django 1.4 development for how to use the correct settings and refer to your Project PATH 请参阅此答案, 在django 1.4开发中使用css,了解如何使用正确的设置并参考您的Project PATH

EDIT (after reading your own answer) 编辑 (阅读自己的答案后)

Guess you are missing this first step: 猜猜您缺少此第一步:

this is the path to your settings.py file: 这是您的settings.py文件的路径:

SETTINGS_DIR = os.path.dirname(os.path.realpath(__file__))

and than this is the path to your project dir: (I Am using buildout, so call it buildout, but it's the root of your project): 并且这是项目目录的路径:(我正在使用buildout,因此称它为buildout,但这是项目的根目录):

BUILDOUT_DIR = os.path.abspath(os.path.join(SETTINGS_DIR, '..'))

and from there on you can define everything you want: 从那里您可以定义所需的一切:

STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'static')
STATIC_URL = '/static_media/'
MEDIA_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'media')
MEDIA_URL = '/media/'

and in your template file refer to the image like: 并在您的模板文件中引用该图像,例如:

<a href="{{MEDIA_URL}}{{ case.zoomOutImage }}"><img src="{{MEDIA_URL}}{{ case.zoomOutImage }}" width="100%"></a>

when your object given to the template is called case 给模板的对象称为大小写

about your question of the urls. 关于您的网址问题。 you should add this: 您应该添加以下内容:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'', include('staticfiles.urls')),
    )

and see the link above to the question about using css, it's the same problem, but there for finding the css files during development. 并查看上面有关使用css的问题的链接,这是相同的问题,但是在开发过程中可以找到css文件。 It's all about the static file places. 全部与静态文件位置有关。

import os
# get abspath
def rel(*x):
   return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
MEDIA_ROOT = rel('media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = '' #if only your static files are in project folder
STATICFILES_DIRS = ( rel('static'),) #if only your static files are in project folder

use this settings, and everything will work 使用此设置,一切都会正常

so i finally solved my problem. 所以我终于解决了我的问题。 For anyone having this issue in the future do the following: 对于以后遇到此问题的任何人,请执行以下操作:

if you're trying to serve static media files locally on the development server use absolute paths for MEDIA_ROOT and MEDIA_URL. 如果您尝试在开发服务器上本地提供静态媒体文件,请为MEDIA_ROOT和MEDIA_URL使用绝对路径。

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

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