尝试访问'/ media / uploads /

时间:2013-10-03 07:15:54

标签: django django-models django-admin django-media django-uploads

我在admin / urls上的上传方法有问题:

在我的设置中:

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = os.path.join(PROJECT_PATH, "media")

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

# 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 = (
    os.path.join(PROJECT_PATH, "static"),
)

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

在我的lms / models.py

MEDIA_TYPES = (
    ('Videos', 'Videos'),
    ('Photos', 'Photos'),
    ('PDF', 'PDF'),
)


class LessonFile(models.Model):
    """
    The files for every lessons
    """
    lesson = models.ForeignKey(Lesson)
    documents = models.FileField(upload_to='/media/uploads/lms/lessons/')
    title = models.CharField(max_length=255)
    media_type = models.CharField(max_length=255, choices=MEDIA_TYPES)

    def __unicode__(self):
        return self.lesson

在我的admin /上,当我尝试保存文档时(通过上传方法):

尝试访问'/media/uploads/lms/lessons/xxxx.pdf'被拒绝。

2 个答案:

答案 0 :(得分:4)

https://docs.djangoproject.com/en/1.5/ref/models/fields/#django.db.models.FileField.upload_to

  

FileField.upload_to     将附加到MEDIA_ROOT设置_

的本地文件系统路径_

IOW,upload_to应该是相对路径。试试这个:

documents = models.FileField(upload_to='uploads/lms/lessons/')

答案 1 :(得分:0)

upload_to参数中删除前导斜杠后尝试。

class LessonFile(models.Model):
    ...
    documents = models.FileField(upload_to='uploads/lms/lessons/')
    ...

更新:删除了upload_to参数中的额外media目录。