Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django图像故障模板中的链接已断开_Django_Image_Templates_Jpeg - Fatal编程技术网

Django图像故障模板中的链接已断开

Django图像故障模板中的链接已断开,django,image,templates,jpeg,Django,Image,Templates,Jpeg,全部, 我已经开始问这个问题了(http://stackoverflow.com/questions/5016864/include-image-files-in-django-templates-shows-broken-link)有几只安塞尔鸟,但不知怎么的,这仍然不能解决我的问题。 因此,我尝试发布我的代码,希望有人能告诉我出了什么问题。。 我似乎错过了什么,但看不到什么: 在settings.py中: MEDIA_ROOT='C:/Users/Tijl/Documents/Program

全部,

我已经开始问这个问题了(http://stackoverflow.com/questions/5016864/include-image-files-in-django-templates-shows-broken-link)有几只安塞尔鸟,但不知怎么的,这仍然不能解决我的问题。 因此,我尝试发布我的代码,希望有人能告诉我出了什么问题。。 我似乎错过了什么,但看不到什么:

在settings.py中:

MEDIA_ROOT=
'C:/Users/Tijl/Documents/Programming/Dashboard/src/DashboardDesign/FigureOnWebSite/templates/images/static'

媒体URL=
'http://localhost:8000/static/“

在URL.py中:

(r'^Point3D/graphics/$', 'FigureOnWebSite.views.graphics'),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
                     {'document_root': settings.MEDIA_ROOT}),
在my Figure.htm中

<img src"{{picture}}" alt = "picture"/>
文件的位置位于: C:\Users\Tijl\Documents\Programming\Dashboard\src\DashboardDesign\FigureOnWebSite\templates\images

如果我将Figure.html更改为

{{picture}

它在
http://127.0.0.1:8000/Point3D/graphics/

希望有人能帮我解决以上问题

非常感谢!
Tijl

这比您之前的问题更容易回答。但你本可以编辑你的老问题

解决方案应该是:将您的媒体根目录和媒体URL更改为

MEDIA_ROOT='C:/Users/Tijl/Documents/Programming/Dashboard/src/DashboardDesign/FigureOnWebSite/templates/images/'
MEDIA_URL='static/'
将views.py更改为:

from django.http import RequestContext
from django.shortcuts import render_to_response
def graphics(request):
  laptop = "laptop.jpg"
  c = {'picture': laptop}
  return render_to_response('FigureOnWebSite/templates/Figure.html',c,RequestContext(request))
将您的figure.html添加到:

<img src="{{MEDIA_URL}}{{picture}}" />  

不知道是否会注意到更改问题,但每个人都非常投入!!我试过你的解决办法。我想我是在下一个错误级别:它仍然显示断开的链接图片,但现在有一个警告:资源被解释为图像,但使用MIME类型text/html传输。我更新了解决方案,以实现代码中一些奇怪的方面。例如,如果您使用MEDIA_ROOT指向…/templates/images/static,它会在那里查找文件,因此当您告诉我们您的文件位于…/templates/images中时,这显然是一个错误。希望能澄清MEDIA_ROOT和MEDIA_URL是关于MEDIA_ROOT指定在硬盘上查找静态文件的位置(绝对路径)的。MEDIA_URL只是告诉Django转到MEDIA_ROOT指定的路径。如果我运行新代码,我会收到另一个错误:必须使用RequestContext实例作为第一个参数调用unbound method update()(改为使用dict实例)。知道为什么会出现这种情况吗?我会看看django是否向我解释了有关ImageFileField的内容再次感谢!为了更清楚一点:如果MEDIA_ROOT='c:/files/”和您的MEDIA_URL='static/”在浏览器中输入“”,django将返回文件“c:/files/images/picture.jpg”(如果存在)。请将您的代码更新为我答案中的更新代码。这就是本网站的内容:致力于解决方案。这不是论坛,问题和答案不必是静态的。
from django.http import RequestContext
from django.shortcuts import render_to_response
def graphics(request):
  laptop = "laptop.jpg"
  c = {'picture': laptop}
  return render_to_response('FigureOnWebSite/templates/Figure.html',c,RequestContext(request))
<img src="{{MEDIA_URL}}{{picture}}" />  
<img src="{{picture.url}}" /> 
http://127.0.0.1:8000/static/laptop.jpg