1

I am using django 1.8 and trying to upload an image through my django blog app, however, I am not seeing the uploaded file in the folder I think I am providing the path incorrectly. Also, I have not set MEDIA_ROOT in settings.py, as I am not clear how to do that and why we need to do that. I have followed django documentation and some other useful websites to gather info on this, however unable to resolve the issue. Please help me with this.

Below is my 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 = '[email protected]'
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"

Please let me know if any other information is required. Kindly help me out of this.

1 Answer 1

1

You need to provide MEDIA_ROOT in your settings.py file. Django appends the upload_to path to MEDIA_ROOT to find the location to save the file. For example, if you have

MEDIA_ROOT = 'E:/django/myblog/blog'

in your settings.py and

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

in your model class, the image will be saved to E:/django/myblog/blog/uploaded-images/.

More details on file field

Sign up to request clarification or add additional context in comments.

2 Comments

Can do as you suggest but "FileSystemStorage" stored in the address regardless of MEDIA_ROOT . (docs.djangoproject.com/en/1.8/topics/files/…). I think that is not right the way indicating the route.
@Tony, Thanks for the response. I have tried as you suggested, however, it did not work. I have added MEDIA_ROOT = E:/django/myblog/blog' and in models.py changed image = models.ImageField(upload_to = 'uploaded_images', default = None, null = True, blank = True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.