在django dev环境中找不到静态图像 [英] can't figure out serving static images in django dev environment

查看:117
本文介绍了在django dev环境中找不到静态图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读文章(和其他几个但是仍然无法弄清楚如何显示图像,除非链接到网络服务上存在的文件被硬编码到html模板中。



我有 urls.py

  ... 
(r'^画廊/(景观)/(?P<路径> .jpg)$',
'django.views.static.serve',{'document_root':settings.MEDIA_URL}),

...

其中景观是我正在尝试的专辑之一显示图像。 (还有其他几个。)



views.py 中,它使用以下代码调用模板:

  ... 
< li>< img src = 160.jpg alt =''title =''/>< /立GT;
...

将html中的图像链接解析为:

  http://127.0.0.1:8000/galleries/landscapes/160.jpg 

settings.py 中我有:

  MEDIA_ROOT ='C:/ siteURL /画廊/'
MEDIA_URL ='http:// some-good-URL /画廊/'

在文件系统中有一个文件C:/siteURL/galleries/landscapes/160.jpg,我确实有相同的文件在 http://some-good-URL/galleries/landscapes/160.jpg



无论我在urls.py - MEDIA_ROOT或MEDIA_URL中使用什么(期望有服务的本地映像或从Web服务器) - 我在浏览器的源代码中得到以下内容:

 < li>< img src = 160.jpg />< / li> 

浏览器中没有图像。


解决方案

这是一篇很长的帖子,基本上总结了我所学到的关于Django的所有内容为了获取静态文件的工作(花了我一段时间才能了解所有不同的部分如何组合在一起)。



为了在您的开发服务器(以及以后的真实服务器)中提供静态图像,您将需要做一些事情(请注意第三和第四个步骤):



设置MEDIA_ROOT



MEDIA_ROOT是一个常量,它告诉Django 物理文件路径(在您的文件系统上)。使用你的例子,MEDIA_ROOT需要设置为'C:/ siteURL /画廊/',就像你写的那样。 MEDIA_ROOT将用于以下步骤之一,这就是为什么我们设置它。



设置MEDIA_URL



MEDIA_URL是您的图像所在的url。换句话说,每当想要获得图像时,要查找的网址以 MEDIA_URL开头。通常这是不是以http开始,因为您从自己的服务器提供服务(我的MEDIA_URL通常设置为/ site_media /,意思是从根域开始,请访问site_media等)



使用MEDIA_URL



MEDIA_URL不工作通过魔法,你实际上必须使用它。例如,当您编写获取文件的HTML时,需要如下所示:

 < li> < img src ={{MEDIA_URL}} / 160.jpg/>< / li> 

看看我如何告诉模板使用MEDIA_URL前缀?最终转换为'http://some-good-URL/galleries/160.jpg'



请注意,为了能够在您的模板中实际使用MEDIA_URL,您将不得不将'django.core.context_processors.media'添加到您的 TEMPLATE_CONTEXT_PROCESSORS 设置在您的settings.py文件中,如果我没有错误。



让您的开发人员服务器静态文件



在真实的环境中,您将配置文件,如static_media等地址,无需通过Django即可。但是在开发环境中,您还需要从Django进行服务,因此您应该将此通用行添加到urls.py文件的末尾:

 如果settings.DEBUG:
#在调试中提供静态文件。
urlpatterns + = patterns('',
(r'^ site_media /(?P< path>。*)$','django.views.static.serve',
{' document_root':settings.MEDIA_ROOT,
'show_indexes':True}),

请注意,使用urlsite_media / *(实际上是我的MEDIA_URL)并将其从MEDIA_ROOT文件夹中提供,这是MEDIA_ROOT设置发挥作用的地方。



最终记录



令我困惑的是,这里的很多东西都是为了方便起见。例如,MEDIA_ROOT仅用于您的调试url模式,告诉Django从哪里加载。而MEDIA_URL只是鼓励您不要在所有HTML文件中放置绝对URL,因为当您决定将文件移动到其他服务器时,您必须手动将其全部更改(而不是仅更改MEDIA_URL常数)



当然,这不是必要的:您可以使用自己的文件夹对调试URL文件进行硬编码,确保静态文件真的在服务器(通过在浏览器中访问),然后使用MEDIA_URL设置将手动编码到HTML文件中,以确保事情正常工作。


I've read the article (and few others on the subject), but still can't figure out how to show an image unless a link to a file existing on a web-service is hard-coded into the html template.

I've got in urls.py:

...        
(r'^galleries/(landscapes)/(?P<path>.jpg)$', 
    'django.views.static.serve', {'document_root': settings.MEDIA_URL}),

...

where 'landscapes' is one of the albums I'm trying to show images from. (There are several more of them.)

In views.py it calls the template with code like that:

...
<li><img src=160.jpg alt='' title='' /></li>
...

which resolves the image link in html into:

http://127.0.0.1:8000/galleries/landscapes/160.jpg

In settings.py I have:

MEDIA_ROOT = 'C:/siteURL/galleries/'
MEDIA_URL = 'http://some-good-URL/galleries/'

In file system there is a file C:/siteURL/galleries/landscapes/160.jpg and I do have the same file at http://some-good-URL/galleries/landscapes/160.jpg

No matter what I use in urls.py — MEDIA_ROOT or MEDIA_URL (with expectation to have either local images served or from the web-server) — I get following in the source code in the browser:

<li><img src=160.jpg /></li>

There is no image shown in the browser.

What am I doing wrong?

解决方案

This is a long post, basically summarizing all the things I learned about Django in order to get static files to work (it took me a while to understand how all the different parts fit together).

To serve static images in your development server (and later, your real server), you're going to have to do a few things (note specifically the third and fourth steps):

Set MEDIA_ROOT

MEDIA_ROOT is a constant which tells Django the physical path of the file (on your filesystem). Using your example, MEDIA_ROOT needs to be set to 'C:/siteURL/galleries/', like you wrote. MEDIA_ROOT is going to be used in one of the following steps, which is why we set it.

Set MEDIA_URL

MEDIA_URL is the "url" at which your images sit. In other words, whenever you want to get an image, the url to look for starts with MEDIA_URL. Usually this is not going to start with "http", since you're serving from your own server (my MEDIA_URL is usually set to '/site_media/', meaning to start from the root domain, then go to site_media etc.)

Use MEDIA_URL

MEDIA_URL doesn't work by magic, you actually have to use it. For example, when you're writing the HTML which gets a file, it needs to look like this:

<li><img src="{{MEDIA_URL}}/160.jpg" /></li>

See how I'm telling the template to use the MEDIA_URL prefix? That eventually translates to 'http://some-good-URL/galleries/160.jpg'.

Note that to be able to actually use the MEDIA_URL in your templates, you're going to have to add the line 'django.core.context_processors.media' to your TEMPLATE_CONTEXT_PROCESSORS setting in your settings.py file, if I'm not mistaken.

Make your dev server serve static files

In a real environment, you will configure files with addresses like "static_media" to be served without going through Django. But in a dev environment, you'll want to server them from Django as well, so you should add this generic line to the end of your urls.py file:

if settings.DEBUG:
# Serve static files in debug.
urlpatterns += patterns('',
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': settings.MEDIA_ROOT,
    'show_indexes' : True}),
)

Note how that takes anything with the url "site_media/*" (which is actually my MEDIA_URL) and serves it from my MEDIA_ROOT folder, which is the place where the MEDIA_ROOT setting comes into play.

Final note

What confused me is that a lot of the things here are for convenience. For example, MEDIA_ROOT is only used in your debug url pattern, to tell Django where to load from. And MEDIA_URL is only there to encourage you not to put in absolute URLs in all your HTML files, because then when you decide to move the files to a different server, you'd have to manually change them all (instead of just changing the MEDIA_URL constant).

Of course, none of this is necessary: you can hard-code the debug url patter with your own folder, make sure that the static files really are being server from the url (by visiting it in your browser), and then hand-code that without using the MEDIA_URL setting into the HTML file, just to make sure things work.

这篇关于在django dev环境中找不到静态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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