Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serving image files from django admin

Tags:

python

django

For a site I'm building (first website from scratch btw so new to django) I want to provide to the administrator a means to deploy images from the admin pages, which will be used to drive the business logic from the customer's perspective. However I want the administrator to be able to view these images from the admin page too. So I want one common images folder to be accessible from app views and the admin view. I currently have only one app named retailFilters.

Now actually deploying some files to my media/images folder isn't a problem, I add a record (specifying an image to upload) on the admin page and sure enough, the files are waiting exactly where I expected to be. I also realise I have to tell django where to serve them from, and from poking around the internet I have my MEDIA_ROOT, MEDIA_URL and urlpatterns defined as:

settings.py

...
parent_dir = os.path.abspath(os.path.dirname(__file__) + '/..')
MEDIA_ROOT = os.path.join(parent_dir, 'media/')
MEDIA_URL = 'media/'

(and INSTALLED_APPS includes django.contrib.staticfiles))

urls.py

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

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

However the response to clicking on them is that admin bails out with a banner error:

"Filter with ID "1/change/media/images/2012-03-29".27.05.jpg" doesn't exist. Perhaps it was deleted?"


My python version is 2.7.10

My django version is 1.11

My Directory structure at the moment (root is project directory) is as follows:

.
├── db.sqlite3
├── manage.py
├── media
│   └── images
│       └── 2012-03-29_22.27.05.jpg
├── NOTES
├── retailFilters
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── fixtures
│   │   └── boltons.json
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── 0002_auto_20170816_1934.py
│   │   ├── 0002_auto_20170816_1934.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tables
│   │   ├── Bolt_On_Group.py
│   │   ├── Bolt_On_Group.pyc
│   │   ├── Bolt_On.py
│   │   ├── Bolt_On.pyc
│   │   ├── Filter.py
│   │   ├── Filter.pyc
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── Order_Payment.py
│   │   ├── Order_Payment.pyc
│   │   ├── Order.py
│   │   ├── Order.pyc
│   │   ├── Payment_Vendor.py
│   │   ├── Payment_Vendor.pyc
│   │   ├── User.py
│   │   └── User.pyc
│   ├── tests.py
│   └── views.py
├── snapify
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── static
    └── retailFilters
        └── media
            └── images

my admin.py has:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from django.contrib.auth.models import User
from django.contrib.auth.models import Group

from tables.Filter import Filter

admin.site.unregister(User)
admin.site.unregister(Group)

<...other un-related admin entries ...>    

@admin.register(Filter)
class FILTER_Admin(admin.ModelAdmin) :
    fields = ('DESCRIPTION', 'FILENAME', 'CATEGORY', 'PRICE')

    #list_display = ('show_image',)

and my model for Filter is:

Filter.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.storage import FileSystemStorage

import os
import pdb

categories = {'T': 'Personalised Template', 
              'C': 'Custom Filter'}
images = 'images'

class Filter(models.Model) :
    FILTER_REFERENCE   = models.IntegerField(primary_key = True)
    FILENAME           = models.ImageField(upload_to = images)
    DESCRIPTION        = models.TextField()
    CATEGORY           = models.CharField(max_length = 1, null = True, choices = [(x, categories[x]) for x in categories])
    PRICE              = models.DecimalField(default = 0.00, max_digits = 5, decimal_places = 2)

    def __unicode__(self) :
        return self.DESCRIPTION

    #def show_image(self) :
    #    #pdb.set_trace()
    #    return '<a href="{0}"><img src="{0}"></a>'.format(self.FILENAME)

    #show_image.allow_tags = True

and the last output from the runserver commandline:

System check identified no issues (0 silenced).
August 16, 2017 - 20:03:42
Django version 1.11.1, using settings 'snapify.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[16/Aug/2017 20:03:44] "GET /admin/ HTTP/1.1" 200 4683
[16/Aug/2017 20:03:47] "GET /admin/retailFilters/filter/ HTTP/1.1" 200 4203
[16/Aug/2017 20:03:47] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:03:55] "POST /admin/retailFilters/filter/ HTTP/1.1" 200 3175
[16/Aug/2017 20:03:57] "POST /admin/retailFilters/filter/ HTTP/1.1" 302 0
[16/Aug/2017 20:03:57] "GET /admin/retailFilters/filter/ HTTP/1.1" 200 3189
[16/Aug/2017 20:03:58] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:04:04] "GET /admin/retailFilters/filter/ HTTP/1.1" 200 3067
[16/Aug/2017 20:04:06] "GET /admin/retailFilters/ HTTP/1.1" 200 3167
[16/Aug/2017 20:04:08] "GET /admin/retailFilters/filter/add/ HTTP/1.1" 200 5420
[16/Aug/2017 20:04:08] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:04:20] "POST /admin/retailFilters/filter/add/ HTTP/1.1" 302 0
[16/Aug/2017 20:04:20] "GET /admin/retailFilters/filter/ HTTP/1.1" 200 4391
[16/Aug/2017 20:04:20] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:04:23] "GET /admin/retailFilters/filter/1/change/ HTTP/1.1" 200 5789
[16/Aug/2017 20:04:23] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:04:25] "GET /admin/retailFilters/filter/1/change/media/images/2012-03-29_22.27.05.jpg/ HTTP/1.1" 302 0
[16/Aug/2017 20:04:25] "GET /admin/retailFilters/filter/1/change/media/images/2012-03-29_22.27.05.jpg/change/ HTTP/1.1" 302 0
[16/Aug/2017 20:04:25] "GET /admin/ HTTP/1.1" 200 5427
[16/Aug/2017 20:04:29] "GET /admin/retailFilters/filter/1/change/ HTTP/1.1" 200 5789

Sorry if this has been asked countless times but I've spent a solid 5 hours scouring the internet on this problem and am still unsuccessful.

Can anyone point out what I'm missing?

like image 797
HexedAgain Avatar asked Aug 16 '17 20:08

HexedAgain


1 Answers

You just need to change the settings.py :

MEDIA_ROOT = os.path.join(parent_dir, 'media/')
MEDIA_URL = '/media/'
#------------^
like image 75
PRMoureu Avatar answered Nov 15 '22 09:11

PRMoureu