我正在尝试上传django中的几个文件。在我当地的机器上我使用djangos内置服务器一切正常,但在我的生产力服务器上我得到这个错误:
[Errno 13] Permission denied: '/static'
关于这个问题有很多问题,但我找不到任何有用的东西。 在我的情况下,它与文件权限无关。我发现问题是django希望将文件保存在我的文件系统的根文件夹中,而不是保存在我网站的根文件夹中。如果我在'/ static'中创建文件夹,那么将在那里创建文件,但是网页上没有显示图像,因为django希望它们在'/var/www/webpage-root/static/...'
我使用模型来存储文件:
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)
并以这种方式保存:
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文件如下所示:
<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两端提供静态文件。
我是否遗漏了配置中的某些内容或者有什么问题?我不知道为什么apache想要在/ static而不是/ var / www / website / static上传文件,但我认为这可能是因为我的apache配置存在问题......
有人有想法或能帮助我吗?
非常感谢
答案 0 :(得分:8)
您上传媒体的Apache配置:
Alias /media /var/www/webpage/webpage/
与您的Django设置不同步:
# 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/'
。
答案 1 :(得分:0)
您需要更改静态文件夹的所有权。使用root用户尝试“sudo chown -R yourusername:yourusername / projectfolder / static”。
答案 2 :(得分:0)
我做同样的错误
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
static_url和static_root在这种情况下具有相同的路径''
选择其他目录,如果出现此错误,请重试
解决了我的问题