django文件上传:[Errno 13]权限被拒绝:'/static'
我正在尝试在Django中上传几个文件.在我在服务器上使用djangos构建的本地机器上,一切正常,但是在我的生产力服务器上,出现此错误:
I am trying to upload several files in django. On my local maching where I use the djangos build in server everything works fine but on my productivity server I get this error:
[Errno 13] Permission denied: '/static'
关于此问题有很多问题,但我发现没有任何帮助. 就我而言,它与文件权限无关.我发现问题是django希望将文件保存在我的文件系统的根文件夹中,而不是我的网站的根文件夹中.如果我在"/static"中创建文件夹,则将在此处创建文件,但是例如图像不会显示在网页上,因为django希望它们在"/var/www/webpage-root/static/..."中
There are many questions about this issue but nothing I found worked for me. In my case it has nothing to do with file permissions. I found out that the problem is that django want save the files in the root folder of my filesystem and not in the root folder of my website. If I create the folder in '/static' the files will be created there but images for example are not shown on the webpage because django expects them in '/var/www/webpage-root/static/...'
我使用模型来存储文件:
I use a model to store files:
class Document(models.Model):
title = models.CharField(max_length=100, blank=True, null=False)
document = models.FileField(upload_to='static/bachelor/documents/', max_length=500, blank=True, null=True)
并以这种方式保存它们:
and save them in this way:
if form.is_valid():
data = request.FILES['document']
doc = Document(document=data)
doc.save()
如此处所述: https://docs.djangoproject.com /en/dev/topics/http/file-uploads/
我使用Apache和mod_wsgi. apache文件如下所示:
I use Apache and mod_wsgi. The apache file looks like this:
<VirtualHost *:80>
ServerAdmin user@webpage.de
ServerName webpage.de
ServerAlias www.webpage.de
DocumentRoot /var/www/webpage
Alias /media /var/www/webpage/webpage/
Alias /static /var/www/webpage/static/
<Directory /var/www/webpage>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /var/www/webpage/apache/webpage.wsgi
<Directory /var/www/webpage>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/webpage-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/webpage-access.log combined
</VirtualHost>
我的网站的设置文件:
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
'/var/www/website/static/',
'/home/michael/Development/website/static/',
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
我必须在STATICFILES_DIRS中设置两个不同的路径,因为我在服务于服务器上的静态文件时遇到了问题.通过这两行,我可以在开发机器和公共服务器runnig apache的两端提供静态文件.
I had to set two different paths in STATICFILES_DIRS because I already had problems to serve static files on my server. With this two lines I can serve static files on both ends, on my development machine and my public server runnig apache.
我错过了配置中的某些内容吗?我不知道为什么apache要在/static而不是/var/www/website/static中上传文件,但我认为可能是因为我的apache配置有问题...
Did I miss something in my configuration or is there something wrong? I dont know why apache wants upload the files in /static instead of /var/www/website/static but I think it can be because of a problem with my apache configuration...
有任何想法或可以帮助我吗?
Has anyone an idea or can help me please?
非常感谢
您上载媒体的Apache配置:
Your Apache configuration for the uploaded media:
Alias /media /var/www/webpage/webpage/
与您的Django设置不同步:
is not in-sync with your Django settings:
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = ''
根据您的Apache配置,您应该具有MEDIA_ROOT = '/var/www/webpage/webpage/'
和MEDIA_URL = '/media/'
.
Based on your Apache configuration you should have MEDIA_ROOT = '/var/www/webpage/webpage/'
and MEDIA_URL = '/media/'
.