2013-09-23 287 views
5

我想在django上传几个文件。在我的本地MACHING,我使用Django的构建服务器一切工作正常,但我的工作效率的服务器上我得到这个错误:django文件上传:[Errno 13] Permission denied:'/ static'

[Errno 13] Permission denied: '/static' 

有这个问题,但没有我发现我工作的许多问题。 在我的情况下,它与文件权限无关。我发现问题是,django希望将文件保存在我的文件系统的根文件夹中,而不是保存在我的网站的根文件夹中。如果我在'/静态'中创建文件夹,那么文件将在那里创建,但图像例如没有显示在网页上,因为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 [email protected] 
    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的两端提供静态文件。

我错过了我的配置中的某些东西,或者有什么问题吗?我不知道为什么阿帕奇想要/静态而不是/ var/www /网站/静态上传文件,但我认为这可能是因为我的Apache配置有问题...

有没有人有想法或可以帮助我好吗?

谢谢各位大大

回答

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/'

+0

谢谢你:) :)))你是对的,它现在的作品。 – michij83

+4

你应该标记这个答案是正确的,然后;) –

0

您需要更改静态文件夹的所有权。用root用户试试“sudo chown -R yourusername:yourusername/projectfolder/static”。

+0

那永远不会工作 –

相关问题