即使右键单击“另存为”时图像可用,数据库中的图像也不会显示在django模板中

时间:2013-09-08 07:32:45

标签: django django-templates django-views

我在开发服务器上,我试图在用户上传的模板中显示图片。

即使可以保存图像,图像也不会显示。当我右键单击"另存为"时,窗口中会弹出文件名。

当我尝试将文件保存到硬盘驱动器时,它会出错。

我检查了媒体文件夹,似乎模型工作正常 - 图片按预期上传到媒体文件夹。此外,我能够从数据库(字符,整数)中检索其他数据 - 我只是在努力处理图片。

我已经恢复了与stackexchange上可用的图像上传相关的所有问题,并对我的代码做了很多小改动 - 但似乎没有什么能帮助我。

models.py

from django.db import models

class Lesson(models.Model):
    phrase = models.CharField(max_length=200)
    progress_bar = models.DecimalField(max_digits=5, decimal_places=2)
    lesson_slug = models.SlugField(max_length=50)
    def __unicode__(self):
        return self.phrase

 class Lesson_Options(models.Model):
     lesson = models.ForeignKey(Lesson)
     option1_photo = models.ImageField(upload_to='images/')
     option1_voice =   models.FilePathField(path="/media/user123/Elements/Projects/PENCIL/lesson/voice/", recursive=True)
     option1_photo_description = models.CharField(max_length=200)

views.py

def detail(request, lesson_id):
le_objects = Lesson.objects.get(pk=lesson_id)
me_options = Lesson_Options.objects.get(pk=lesson_id)
lesson = le_objects.progress_bar
display_test = me_options.option1_photo
return render(request, 'lesson/detail.html', {'lesson': lesson, 'display_test':   display_test})

urls.py(项目网址)

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^lesson/', include('lesson.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

urls.py(应用程序网址)

from django.conf.urls import patterns, url
from django.conf import settings
from django.conf.urls.static import static

from lesson import views

urlpatterns = patterns('',
    # ex: /lesson/
    url(r'^$', views.index, name='index'),
    # ex: /lesson/5/
    url(r'^(?P<lesson_id>\d+)/$', views.detail, name='detail'),
    # ex: /lesson/5/results/
    url(r'^(?P<lesson_id>\d+)/results/$', views.results, name='results'),
)

settings.py

MEDIA_ROOT = '/media/user123/Elements/Projects/PENCIL/lesson/media/'

MEDIA_URL = '/media/'

detail.html

<li class="span4"> <a class="thumbnail"> <img alt="hello"
src="{{ display_test }}" height="300" width="300" class="option1" id="incorrect_answer1"></a> </li>

HTML输出

<img alt="hello" src="images/girl_1.jpg" height="300" width="300" class="option1" id="incorrect_answer1">

2 个答案:

答案 0 :(得分:3)

项目urls.py文件丢失:

if settings.DEBUG:
    urlpatterns += patterns(
        '',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    )

正确代码:

models.py

from django.db import models

class Lesson(models.Model):
    phrase = models.CharField(max_length=200)
    progress_bar = models.DecimalField(max_digits=5, decimal_places=2)
    lesson_slug = models.SlugField(max_length=50)
    def __unicode__(self):
        return self.phrase

 class Lesson_Options(models.Model):
     lesson = models.ForeignKey(Lesson)
     option1_photo = models.ImageField(upload_to='images/')
     option1_voice =   models.FilePathField(path="/media/user123/Elements/Projects/PENCIL/lesson/voice/", recursive=True)
     option1_photo_description = models.CharField(max_length=200)

views.py

def detail(request, lesson_id):
le_objects = Lesson.objects.get(pk=lesson_id)
me_options = Lesson_Options.objects.get(pk=lesson_id)
lesson = le_objects.progress_bar
display_test = me_options.option1_photo
return render(request, 'lesson/detail.html', {'lesson': lesson, 'display_test':   display_test})

urls.py(项目网址)

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^lesson/', include('lesson.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += patterns(
        '',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    )

urls.py(应用程序网址)

from django.conf.urls import patterns, url
from django.conf import settings
from django.conf.urls.static import static

from lesson import views

urlpatterns = patterns('',
    # ex: /lesson/
    url(r'^$', views.index, name='index'),
    # ex: /lesson/5/
    url(r'^(?P<lesson_id>\d+)/$', views.detail, name='detail'),
    # ex: /lesson/5/results/
    url(r'^(?P<lesson_id>\d+)/results/$', views.results, name='results'),
)

settings.py

MEDIA_ROOT = '/media/user123/Elements/Projects/PENCIL/lesson/media/'

MEDIA_URL = '/media/'

detail.html

<li class="span4"> <a class="thumbnail"> <img alt="hello"
src="{{ display_test }}" height="300" width="300" class="option1" id="incorrect_answer1"></a> </li>

HTML输出

<img alt="hello" src="images/girl_1.jpg" height="300" width="300" class="option1" id="incorrect_answer1">

答案 1 :(得分:1)

这是documentation for FileField。看来你已经完成了前两个步骤了。尝试按照第三步。而不是display_test = me_options.option1_photo放置:

display_test = me_options.option1_photo.url

或直接在模板中试用:

<img alt="hello" src="{{ display_test.url }}" height="300" width="300" class="option1" id="incorrect_answer1">