简体   繁体   English

在Django中提供图片的问题

[英]Problem with serving pictures in Django

I'm trying to create my first site in django and I've run into a problem. 我正在尝试在django中创建我的第一个站点,但遇到了问题。 I'm trying to serve pictures,but it isn't working correctly. 我正在尝试提供图片,但是无法正常工作。 I have it set up in the following way: 我通过以下方式进行设置:
In settings.py: 在settings.py中:

MEDIA_ROOT = 'C:/Users/John/Documents/My Dropbox/Infostuff/filmsite/media/'
MEDIA_URL = 'localhost:8000/static/'  
ADMIN_MEDIA_PREFIX = '/admin-media/'

In urls.py: 在urls.py中:

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

It looks like this in the template page: 在模板页面中看起来像这样:

<img src="{{MEDIA_URL}}{{a.picture.url}}">

And it is parsed to this: 它被解析为:

<img src="localhost:8000/static/portrets/0000138_Leonardo_D.jpg">

When I open the html page it doesn't display the pictures (I get the broken picture icon). 当我打开html页面时,它不显示图片(我得到了损坏的图片图标)。 If I however go to view the source and copy the above url and past it directly into my browser it does load the picture. 如果我然而去查看源和上面的网址和过去,它直接复制到我的浏览器加载图片。 What am I doing wrong? 我究竟做错了什么?

I'm using Django 1.2. 我正在使用Django 1.2。 I'm not using Apache yet, because I would first like to get it working in a development environment. 我尚未使用Apache,因为我首先要使其在开发环境中工作。

PS: This is the first time I'm asking a question on this site, if I did something wrong, please tell. PS:这是我第一次在此网站上提问,如果我做错了,请告知。

What version of Django are you using? 您正在使用哪个版本的Django?

Can you post the generated HTML code? 您可以发布生成的HTML代码吗? if the URL works when you copy and paste in the browser, it might be an html issue as well. 如果在复制和粘贴到浏览器中时URL有效,则也可能是html问题。

Have you looked at this page yet? 你看过这个页面了吗?

http://docs.djangoproject.com/en/dev/howto/static-files/ http://docs.djangoproject.com/en/dev/howto/static-files/

Update: 更新:

Can you post the model for a? 您可以为张贴模型吗? is a.picture an image field? 图片是图像场吗? If so, then you don't need to put the MEDIA_URL in your img src, since it is already an absolute url and it should include the MEDIA_URL already. 如果是这样,则您无需将MEDIA_URL放入img src中,因为它已经是一个绝对URL,并且应该已经包含MEDIA_URL。 try removing that and see if it works. 尝试删除它,看看是否可行。

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

For more info see this page. 有关更多信息,请参见此页面。

http://docs.djangoproject.com/en/1.3/ref/models/fields/#django.db.models.FileField.storage http://docs.djangoproject.com/zh-CN/1.3/ref/models/fields/#django.db.models.FileField.storage

Your MEDIA_URL should just be '/static/' - or, if you must, 'http://localhost:8000/static/' . 您的MEDIA_URL应该只是'/static/' -或(如果必须的话)是'http://localhost:8000/static/' Otherwise your browser is interpreting the localhost as part of the path, not the domain. 否则,您的浏览器会将localhost解释为路径的一部分,而不是域的一部分。

use: 采用:

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

or 要么

MEDIA_URL = '/static/' 

Ok, I feel very stupid... I had to change 好吧,我感到非常愚蠢...我不得不改变

MEDIA_URL = 'localhost:8000/static/' 

to

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

and then I had change 然后我有了改变

<img src="{{MEDIA_URL}}{{a.picture.url}}">

to

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

Thank you for your time. 感谢您的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM