1

I want to attach pictures in my application django 1.8. When I attach a file, I have a link to it in the admin panel. After I click on the link I get error code 404.

My url: http://127.0.0.1:8000/media/paint/2015/04/15/1.png

My settings

"""
Django settings for website project.

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

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

# 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 = '6tmda(mjlwf%kl1*r@=6s0*#ozpvu&89@hlkcj9r2+(euk4kq#'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'suit',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
    'www',
    'tinymce',
    'crispy_forms',
    'bootstrap_pagination',
)

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 = 'website.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
)

WSGI_APPLICATION = 'website.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/

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'public_assets')

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

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

My model

class Paint(models.Model):
    AVAILABLE = "Available"
    NOT_AVAILABLE = "Not available"
    STATUS_PAINT = (
        (AVAILABLE, "Available"),
        (NOT_AVAILABLE, "Not available")
    )
    title = models.CharField(max_length=200)
    gallery = models.OneToOneField(Gallery)
    paint = models.ImageField(upload_to='paint/%Y/%m/%d')
    price = models.CharField(max_length=50, blank=True, null=True)
    status = models.CharField(choices=STATUS_PAINT, default=AVAILABLE, max_length=50)

    class Meta:
        verbose_name = "Picture"
        verbose_name_plural = "Images"

    def __unicode__(self):
        return "{}".format(self.title)
mark
  • 653
  • 1
  • 10
  • 23
  • Does the file exist on your filesystem where you expect it to? – Dominic Rodger Apr 15 '15 at 11:51
  • @DominicRodger yes, folder paint with subfolders exists, and file 1.png too. – mark Apr 15 '15 at 11:57
  • possible duplicate of [Django MEDIA\_URL and MEDIA\_ROOT](http://stackoverflow.com/questions/5517950/django-media-url-and-media-root) – Aylen Apr 15 '15 at 12:18
  • @Filly Please note that the accepted answer in that question demonstrates an older method. The current method is to modify `urls.py` as in my answer. – Selcuk Apr 15 '15 at 12:20

1 Answers1

3

You must configure your development server to serve uploaded files:

For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Please note that this is only suggested for development use. For production, you should configure your web server (Apache, NginX etc) to handle static and media folders.

Selcuk
  • 57,004
  • 12
  • 102
  • 110