Django图像故障链接在模板中 [英] Django Image trouble broken link in template

查看:108
本文介绍了Django图像故障链接在模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部



我已经开始了这个问题(http://stackoverflow.com/questions/5016864/include-image-files-in-django-templates -shows-broken-link)与几个有用的anserws,但不知何故,这仍然不能解决我的麻烦。
因此,我尝试发布我的代码,希望有人能够告诉我什么问题..
我似乎错过了一些,但没有看到什么:


$ b $ c:/ Users / Tijl / Documents / Programming / Dashboard / src / DashboardDesign / C / C / C / C /图onWebSite / templates / images / static'



MEDIA_URL = 'http:// localhost:8000 / static /



在urls.py:

 (r'^ Point3D / graphics / $','FigureOnWebSite.views.graphics'),
(r'^ static /(?P< path>。*)$','django.views.static。 ,
{'document_root':settings.MEDIA_ROOT}),

py:

 > def图形(请求):
> laptop =laptop.jpg
> t = loader.get_template('FigureOnWebSite / templates / Figure.html')
> c =上下文({'picture':laptop})
>返回HttpResponse(t.render(c))

在我的图.htm;

 < img src{{picture}}alt =picture/> 

它显示了一个断开的链接图像,如果我右键单击我可以看到正确的来源,但仍然是'显示链接是:

  http://127.0.0.1:8000/Point3D/graphics/ 

文件的位置在:
C:\Users\Tijl\Documents\Programming \Dashboard\src\DashboardDesign\FigureOnWebSite\templates\images



如果我将Fig.html更改为;



{{picture}}



它显示 laptop.txt http://127.0.0.1:8000/Point3D/graphics/



希望以上是足够的有人帮我出去。



谢谢你!
Tijl

解决方案

这可以比您以前的问题更容易回答。但是你可以编辑你的旧问题。



解决方案应该是:更改您的MEDIA_ROOT& MEDIA_URL到

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

将您的views.py更改为: / p>

从django.http导入RequestContext 
从django.shortcuts导入render_to_response
def图形(请求):
laptop =laptop.jpg
c = {'picture':laptop}
return render_to_response('FigureOnWebSite / templates / Figure.html',c,RequestContext(request))

和你的figure.html到:

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

注意:如果您在模型中使用ImageFileField,这将会更容易。你可以写:

 < img src ={{picture.url}}/> 

您收到的网址是不是的正确网址,它在 http://127.0.0.1:8000/ 之后缺少静态/。您的正确网址必须是:

  http://127.0.0.1:8000/static/laptop.jpg 


All,

I've started this question (http://stackoverflow.com/questions/5016864/include-image-files-in-django-templates-shows-broken-link) with several helpful anserws but somehow this still doesn't resolve my trouble. Therefore i try posting my code and hopefully someone is able to tell me whats going wrong.. I seem to miss something but don't see what:

in settings.py :

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

MEDIA_URL = 'http://localhost:8000/static/'

In urls.py:

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

in views.py:

> def graphics(request):
>     laptop = "laptop.jpg"
>     t = loader.get_template('FigureOnWebSite/templates/Figure.html')
>     c = Context({'picture': laptop})
>     return HttpResponse(t.render(c))

in my Figure.htm;

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

it shows a broken link image and if i right click i can see the right source but still isn't displaying it the link is:

http://127.0.0.1:8000/Point3D/graphics/

the location of the file is in: C:\Users\Tijl\Documents\Programming\Dashboard\src\DashboardDesign\FigureOnWebSite\templates\images

If I change Figure.html to;

{{picture}}

it shows laptop.txt on http://127.0.0.1:8000/Point3D/graphics/

Hopefully the above is enough for someone to help me out.

Thanks alot! Tijl

解决方案

This can be answered more easily than your previous question. But you could have just edited your old question.

Solution should be: change your MEDIA_ROOT & MEDIA_URL to

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

change your views.py to:

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

and your figure.html to:

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

notice: if you would use an ImageFileField in your model, this would all be a lot easier for you. You could just write:

<img src="{{picture.url}}" /> 

and the url you are getting returned is not the "right" url, it is missing the static/ after http://127.0.0.1:8000/. Your correct url must be:

http://127.0.0.1:8000/static/laptop.jpg

这篇关于Django图像故障链接在模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆