Django图像在模板中发生故障链接

时间:2022-10-29 21:19:31

All,

I've started this question (http://*.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:

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

in settings.py :

在settings.py中:

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

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

MEDIA_URL = 'http://localhost:8000/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;

在我的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

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

If I change Figure.html to;

如果我将Figure.html更改为;

{{picture}}

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

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

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

希望以上内容足以帮助我。

Thanks alot! Tijl

非常感谢! Tijl

1 个解决方案

#1


0  

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和MEDIA_URL更改为

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

change your views.py to:

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

and your figure.html to:

和你的figure.html:

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

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

<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/之后缺少静态/。您的正确网址必须是:

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

#1


0  

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和MEDIA_URL更改为

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

change your views.py to:

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

and your figure.html to:

和你的figure.html:

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

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

<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/之后缺少静态/。您的正确网址必须是:

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