Django媒体和模板中的图像问题

时间:2016-03-16 15:05:49

标签: python django

有问题,尝试在django中做简单的画廊,但是当我试图查看它的外观时,图片并没有显示任何标题名称,而且在终端"找不到错误404&# 34 ;.无法实现真正​​的错误......请帮助。 THX ...

未找到:/media/images/example.png [16 / Mar / 2016 17:59:17]" GET /media/images/example.png HTTP / 1.1" 404 2168

models.py

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')
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True, null=True)

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

urls.py - app

from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import ListView, DetailView

from .models import Album, Photo

urlpatterns = [
  url(r'^$', ListView.as_view(
    model=Album,
    context_object_name='my_album',
    template_name='gallery/album.html'),
    name='gallery'
    ),
  url(r'^(?P<slug>[-\w]+)/$', DetailView.as_view(
    model=Album,
    context_object_name='photos',
    template_name='gallery/photo.html'),
    name='photo'
    ),
]


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

urls.py - 项目

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

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

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

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

模板

{% extends "gallery/base.html" %}
{% load thumbnail %}
{% block title %}My Gallery{% endblock %}

{% block text %} 
    {% for album in my_album %}

        <a href='{{ album.slug }}'>
        <img src="{{ album.img.url}}" alt="{{ album.title }}">

          <!-- {% thumbnail album.img "200x200" crop="center" as im %}
            <img src="{{ im.url }}" alt="{{ im.title }}" width="{{ im.width }}" 
        height="{{ im.height }}">
            {% endthumbnail %} -->

        </a>
    {% endfor %}
{% endblock %}

ProjectStructure

ProjectFolder/
    app/
        migrations/
        static/
        templates/
        admin.py
        models.py
        ..
    project/
        settings.py
        urls.py
        ..
    media/
        images/
            example.png
    my_env/
    manage.py

如果在setting.py我做MEDIA_URL =&#39;媒体/图片&#39;它会工作,但只有专辑模板,如果你去看专辑照片,那里的照片也会显示同样的错误...实际上我无法理解并意识到如何访问文件夹媒体,只需将任何内容放在这里我需要的东西。帮帮我...

0 个答案:

没有答案