2013-09-23 285 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

那永遠不會工作 –

相關問題