无法使用django上传文件夹中的图像

时间:2016-01-19 17:29:53

标签: python django

我正在使用django 1.8并尝试通过我的django博客应用程序上传图像,但是,我没有看到文件夹中的上传文件我认为我提供的路径不正确。另外,我还没有在settings.py中设置MEDIA_ROOT,因为我不清楚如何做到这一点以及为什么我们需要这样做。 我已经关注了django文档和其他一些有用的网站来收集有关这方面的信息,但无法解决问题。请帮帮我。

以下是我的models.py

from django.db import models
from django.contrib.auth.models import User
from django.core.files.storage import FileSystemStorage

fs = FileSystemStorage(location='E:\django\myblog\\blog\uploaded images')
# Create your models here.
class BlogPost(models.Model):
    title = models.CharField(max_length = 30)
    posted_by = models.CharField(max_length = 30)
    posted_on = models.DateTimeField(auto_now_add = True)
    description = models.CharField(max_length = 200)
    comment = models.CharField(max_length = 150) 
    tags = models.CharField(max_length=50, default = "notag")
    image = models.ImageField(upload_to = fs, default = None, null = True, blank = True)

    def __str__(self):
        #return "{0} : {1}".format(self.title, self.description)
        return self.title

settings.py

"""
Django settings for myblog project.

Generated by 'django-admin startproject' using Django 1.8.4.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES_DIRS = (os.path.join(BASE_DIR, "templates"),)


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'u6cawf)%jq=fd-z(!)z-@2%-ti8bb5&4xl_49zwrd@-b-m*5a*'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'blog',
    'crispy_forms',
    'django.contrib.sites',
    'django_comments',
    'my_comments_app',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',    

)

SITE_ID = 1
COMMENTS_APP = 'my_comments_app'

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'myblog.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        #'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders':[
                ('django.template.loaders.cached.Loader',[                
                'django.template.loaders.app_directories.Loader',
                'django.template.loaders.filesystem.Loader', 
                ]),
            ],
        },
    },
]

WSGI_APPLICATION = 'myblog.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

##Email settings
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'sekhar.sourav8@gmail.com'
EMAIL_HOST_PASSWORD = 'souravsekharsahoo'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)


LOGIN_URL = "blog_login"
LOGOUT_URL = "blog_logout"
LOGIN_REDIRECT_URL = "blog_home"

如果需要任何其他信息,请告诉我。请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您需要在settings.py文件中提供MEDIA_ROOT。 Django将upload_to路径附加到MEDIA_ROOT以查找保存文件的位置。例如,如果你有

MEDIA_ROOT = 'E:/django/myblog/blog'
在您的settings.py和

image = models.ImageField(upload_to = 'uploaded-images', default = None, null = True, blank = True)

在您的模型类中,图像将保存到 E:/ django / myblog / blog / uploaded-images /

More details on file field