Brian Lee
Brian Lee

Reputation: 305

can't get right url to serve admin-uploaded image django

I'm trying to get a template to display images uploaded through the admin interface, and I've had zero luck with it. I've tried looking through the pre-existing questions on this topic, and none have helped.

Below is my settings, models, view, and template file

settings.py

import os
from os import path
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))






DEBUG = True

ALLOWED_HOSTS = []
PROJECT_DIR_PATH = path.dirname(path.normpath(path.abspath(__file__)))




INSTALLED_APPS = [
    'about.apps.AboutConfig',
    'forms.apps.FormsConfig',
    'resources.apps.ResourcesConfig',
    'contact.apps.ContactConfig',
    'blog.apps.BlogConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'nydkcd11.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
            ],
        },
    },
]

WSGI_APPLICATION = 'nydkcd11.wsgi.application'




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




AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True



MEDIA_ROOT = 'media/'
MEDIA_URL = '/media/'


STATIC_URL = '/static/'

models.py

from django.db import models
class Division(models.Model):
    name = models.CharField(max_length = 100)
    school = models.CharField(max_length = 75)
    position = models.CharField(max_length = 50)
    desc = models.TextField()
    image = models.ImageField(upload_to='images/')
    def __str__(self):
        return self.name

views.py

def division(request):
    member_list = Division.objects.all()
    return render(request, 'about/division.html',{'member_list':member_list})
    return HttpResponse("this is the divison page")

division.html

<h1>Divisional Board Members</h1>
{% for member in member_list%}
    <h2>{{member.name}}</h2>
    <h3>{{member.school}}</h3>
    <h3>{{member.position}}</h3>
    <img src = "{{member.image.url}}">
    <p>{{member.desc}}</p>
    <p>{{MEDIA.ROOT}}</p>
    <p>{{MEDIA.URL}}</p>
{% endfor %}

The specific problem that comes up is that the full directory is not generated. For example, the url I'll get will be media/images/example.jpg, but doesn't include the full absolute path. This has (unsurprisingly) prevented http://127.0.0.1:8000/media/images/example.jpg from working. Could someone demonstrate how I should go about getting the full path for the template to use?

Upvotes: 1

Views: 740

Answers (1)

Exprator
Exprator

Reputation: 27533

from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
]

urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

put this in your urls.py

Upvotes: 1

Related Questions