R
R
Ruslan Bergutov2015-04-28 15:12:47
Django
Ruslan Bergutov, 2015-04-28 15:12:47

Why is the image not being picked up in Django?

myapp/models.py

#! coding: utf-8
from django.db import models

# Альбом с фотографиями
class Album(models.Model):
    title = models.CharField("Название альбома", max_length=100)
    slug = models.SlugField("Ссылка для альбома", max_length=100, unique=True)
    img = models.ImageField("Изображение альбома", upload_to='images',
                            help_text='Размер изображения 200px на 200px')

    class Meta:
        ordering = ['title']
        verbose_name = 'Альбом'
        verbose_name_plural = 'Альбомы'

    def __unicode__(self):
        return self.title
    def __str__(self):
        return self.title


class Photo(models.Model):
    title = models.CharField("Название фотографии", max_length=100)
    album = models.ForeignKey(Album, verbose_name='Альбом')
    img = models.ImageField("Фото", upload_to='images',
                            help_text='Желательно, чтоб фото было не большого размера')

    class Meta:
        ordering = ['title']
        verbose_name = 'Фото'
        verbose_name_plural = "Фотографии"

    def __unicode__(self):
        return self.title
    def __str__(self):
        return self.title
    def image_thumb(self):
        return '<img src="/media/%s" width="100" height="100" />'  % (self.img)
    image_thumb.allow_tags = True

myapp/admin.py
from django.contrib import admin
from photos.models import Album, Photo
#
#Для альбома
class AlbumAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['title', 'slug', 'img']})
    ]
    list_display = ['title', 'slug']
    prepopulated_fields = {'slug': ['title']}
    ordering = ['title']
#
#Для фотографии
class PhotoAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {"fields": ['title', 'album', 'img']})
    ]
    list_display = ['title', 'album', 'image_thumb']
    ordering = ['title']


admin.site.register(Album, AlbumAdmin)
admin.site.register(Photo, PhotoAdmin)

settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
...
MEDIA_ROOT = '/home/hivetraum/Dropbox/Работа/Галовед/galoved/galoved/media/'
MEDIA_URL = '/media/'

The problem itself:
For some reason, the uploaded images are not available and, accordingly, are not displayed in the admin
panel. I tried a bunch of snippets with thumbnails in the admin panel, everywhere the result is approximately the same
7ca3866c74644514b11616fe1d122100.png5e2f4da247424ba19204d6bd31a7aa3b.png

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey K, 2015-04-28
@mututunus

urls.py

from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

S
sim3x, 2015-04-28
@sim3x

janga subdefault does not deal with your static

from settings import DEBUG, MEDIA_URL, MEDIA_ROOT
if DEBUG:
    urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)
    urlpatterns += staticfiles_urlpatterns()

M
marazmiki, 2015-04-28
@marazmiki

The guys, in general, said everything correctly about the distribution of statics. I would like to supplement the topic with another solution that I like more. It consists in using wsgi-middleware dj-static .
To connect statics, you need to add to the wsgi.py of the project

from django.core.wsgi import get_wsgi_application
from dj_static import Cling, MediaCling

application = Cling(MediaCling(get_wsgi_application()))

and that's it. You don't need to edit urls.py.
Obvious pluses - this thing can distribute static even in a production environment. Those who have hosted django projects on heroku hosting do just that. And, as mentioned above, you don't need to add additional developer url schemes to urls.py.
Obvious disadvantages - additional dependence (although is it really a minus?). Installed via pip:
$ pip install dj-static

U
un1t, 2015-04-28
@un1t

runserver distributes static by default, but does not distribute media. It must be added to urls.py as written above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question