无法在中使用imageField显示照片模型.py我的Django应用程序?

2024-09-27 07:18:22 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的模型.py在我的Django PhotoViewer应用程序中,我为Photo类定义了一个imageField,用于在站点上显示图像。我上传了一些照片通过Django管理页面,但没有一个显示出来。我确实看到所有这些照片上传到我的/static/images文件夹。但当我转到我的照片索引(照片列表页):127.0.0.1:8000:photoViewer/photos时,我看到的是下面的页面:

enter image description here

models.py

from django.db import models
#photoViewer/: (index) photostream (list all photos) 
class Photo (models.Model):
    photo_title = models.CharField(max_length=200)
    #how to retrieve from metadata of file?
    date_taken = models.DateField('date taken', default=None, blank=True, null=True)
    photo_img = models.ImageField(upload_to = "images/", default= "")
    def __str__(self):              # __unicode__ on Python 2
        return self.photo_title

光电观察器/网址.py在

^{pr2}$

光电观察器/视图.py在

from django.shortcuts import get_object_or_404, render
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader 
from photoViewer.models import Photo, Album
from django.core.urlresolvers import reverse
from django.views import generic

# Create your views here.

class TemplateTestView (generic.ListView):
    model = Photo
    template_name = 'photoViewer/test.html'

class IndexView(generic.ListView):
    #By default: Uses <app name>/<model name>_detail.html
    #as template
    template_name = 'photoViewer/photo_index.html'
    context_object_name = 'latest_photo_list'
    def get_queryset(self):
        return Photo.objects.order_by('-date_taken')[:5]

class DetailView(generic.DetailView):
    model = Photo
    #By default: Uses <app name>/<model name>_detail.html
    #Unless specified by template_name
    template_name = 'photoViewer/photo_detail.html'
    #context_object_name = <new name> 


class AlbumsIndexView(generic.ListView):
    template_name = 'photoViewer/albums_index.html'
    context_object_name = 'latest_albums_list'
    def get_queryset(self):
        return Album.objects.order_by('-date_created')[:5]


class AlbumDetailView(generic.DetailView):
    model = Album
    template_name = 'photoViewer/albums_detail.html'

photoViewer/templates/photoViewer/photo_detail.html(照片详细信息页面模板):

<h1>Photo Detail</h1>
<li>{{ photo.photo_title }}</li>
<li>{{ photo.date_taken }}</li>
<img src="/static/{{ photo.photo_img }}" alt={{ photo.photo_title }}>

<h2>Albums Association</h2>
{% if photo.album_set.all %}
<ul>
    {% for album in photo.album_set.all %}
        <li>{{ album.album_title }}</li>


        <!-- <li><a href="{% url 'photoViewer:detail' photo.id %}">{{ photo.photo_title }}</a>    {{ photo.date_taken }} </li>
 -->
    {% endfor %}
</ul>
{% else %}
    <p>This Photo is Not in Any Album</p>
{% endif %}

我安装了pillow的最新版本


Tags: djangonamefromimportdatetitlemodelshtml
3条回答

无论何时使用模型对象显示图像,都需要使用object.photo_img.url来访问图像路径。但在此之前,请确保照片图像不包含空字符串。在

[编辑] 除非您向网址.pyDjango文档中提到的条目

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_ROOT, document_root=settings.MEDIA_ROOT)

参考号:https://docs.djangoproject.com/en/1.7/howto/static-files/#serving-static-files-during-development

试试这个,在设置.py

STATIC_URL = '/static/'

MEDIA_ROOT = '/home/subhanshu/mysite/static/'     #put the absolute path 

MEDIA_URL =   '/home/subhanshu/mysite/static/'    #put the absolute path

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

并对模板进行如下更改:

^{pr2}$

如果您有一些要显示的静态图像,可以使用这个:

<img src="{% static 'img/example.jpg' %}">

在模板中使用{STATIC{URL}},而不是硬编码“/STATIC/”。在

设置STATIC_ROOT以在开发和服务器环境中正确地为静态文件提供服务。在

相关问题 更多 >

    热门问题