1

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

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

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

我检查了媒体文件夹,似乎模型工作正常 - 图片已按预期上传到媒体文件夹。此外,我能够从数据库中检索其他数据(字符、整数)——我只在图片方面挣扎。

我已经恢复了与 stackexchange 上可用的图像上传相关的所有问题,并对我的代码进行了许多小的更改 - 但似乎没有任何帮助。

模型.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)

视图.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'),
)

设置.py

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

MEDIA_URL = '/media/'

详细信息.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">
4

2 回答 2

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}),
    )

正确代码:

模型.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)

视图.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'),
)

设置.py

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

MEDIA_URL = '/media/'

详细信息.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">
于 2013-09-11T17:29:09.493 回答
1

这是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">
于 2013-09-10T14:10:24.430 回答