2013-09-23 71 views
5

Tôi đang cố tải lên một số tệp trong django. Trên maching địa phương của tôi mà tôi sử dụng djangos xây dựng trong máy chủ tất cả mọi thứ hoạt động tốt nhưng trên máy chủ năng suất của tôi, tôi nhận được lỗi này:Tải lên tệp django: [Errno 13] Quyền bị từ chối: '/ static'

[Errno 13] Permission denied: '/static' 

Có rất nhiều câu hỏi về vấn đề này nhưng không tìm thấy tôi làm việc cho tôi. Trong trường hợp của tôi, nó không liên quan gì đến quyền truy cập tập tin. Tôi phát hiện ra rằng vấn đề là django muốn lưu các tập tin trong thư mục gốc của hệ thống tập tin của tôi và không có trong thư mục gốc của trang web của tôi. Nếu tôi tạo thư mục trong '/ static', các tệp sẽ được tạo ở đó nhưng hình ảnh chẳng hạn được hiển thị trên trang web vì django dự kiến ​​chúng trong '/ var/www/site-root/static/...'

tôi sử dụng một mô hình để lưu trữ tập tin:

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) 

và lưu chúng theo cách này:

if form.is_valid(): 
    data = request.FILES['document'] 
    doc = Document(document=data) 
    doc.save() 

Như đã trình bày ở đó: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/

tôi sử dụng Apache và mod_wsgi. Các tập tin apache trông như thế này:

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

Các thiết lập tập tin của trang web của tôi:

# 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', 
) 

tôi đã phải thiết lập hai con đường khác nhau trong STATICFILES_DIRS bởi vì tôi đã có vấn đề để phục vụ các tập tin tĩnh trên máy chủ của tôi. Với hai dòng này, tôi có thể phục vụ các tệp tĩnh trên cả hai đầu, trên máy phát triển của tôi và máy chủ công cộng của tôi runnig apache.

Tôi đã bỏ lỡ điều gì đó trong cấu hình của mình hoặc có vấn đề gì không? Tôi không biết tại sao apache muốn tải lên các tập tin trong/tĩnh thay vì/var/www/website/tĩnh nhưng tôi nghĩ rằng nó có thể là do một vấn đề với cấu hình apache của tôi ...

Có ý tưởng hay có thể giúp tôi xin vui lòng?

Cảm ơn bạn nhiều

Trả lời

8

cấu hình Apache của bạn cho các phương tiện truyền thông tải lên:

Alias /media /var/www/webpage/webpage/ 

không có trong động đồng bộ hóa với các thiết lập Django của bạn:

# 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 = '' 

Dựa vào cấu hình Apache của bạn, bạn nên có MEDIA_ROOT = '/var/www/webpage/webpage/'MEDIA_URL = '/media/'.

+0

Cảm ơn bạn nhiều :)))) Bạn đã đúng và nó hoạt động ngay bây giờ. – michij83

+4

Bạn nên đánh dấu câu trả lời này là đúng;) –

0

Bạn cần thay đổi quyền sở hữu của thư mục tĩnh. Với người dùng root, hãy thử "sudo chown -R tên người dùng: yourusername/projectfolder/static".

+0

Điều đó không bao giờ có thể hoạt động –