在静态文件中加载图像 [英] Loading images in a static file

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

问题描述

所以我已经完成了Django教程,玩了一些,觉得我对Django有了很好的了解。我唯一遇到的问题是,当我尝试显示登陆页面的静态页面时,似乎无法加载任何图像。

So I have completed the Django tutorial, play around with it some, and feel that I have a good understanding of Django. The only problem I am facing is that when I try to display a static page for a landing page, I cannot seem to get any images to load.

首先我有尝试了两种不同的显示静态着陆页的方法。
首先:

First off I have tried two different methods of displaying a static landing page. First:

# main app urls.py
    from django.conf.urls import patterns, include, url

    from django.contrib import admin
    admin.autodiscover()

    urlpatterns = patterns('',
        url(r'^$', TemplateView.as_view(template_name="landing/index.html")),
        url(r'^polls/', include('polls.urls', namespace="polls")),
        url(r'^admin/', include(admin.site.urls)),
    )  

这是主应用程序的urls.py,我使用TemplateView显示位于'my_app / templates / landing / index.html'的静态index.html。

This is the main app's urls.py and I use the TemplateView to display the static index.html which is located at 'my_app/templates/landing/index.html'.

这是很好,直到我尝试添加静态图像到index.html文件。无论我把图像文件放在哪里,我都看不到它显示。在index.html模板中,我尝试过使用不同的静态方法以及尝试不同的直接路径,而不需要嵌入的Python代码。我应该如何显示图像以及它们应该位于哪里?

This is all and well until I try to add a static image into the index.html file. No matter where I put the image file I cannot seem to get it to display. In the index.html template I have tried different methods of using static as well as trying different direct paths without the need for the embedded python code. How am I supposed to display images and where should they be located?

我发现只是显示静态页面(而不是图像)的第二种方法是创建一个名为landing的新应用程序,并以相同的方式(使用TemplateView)从urls.py中简单地显示一个静态页面。这种方法更好吗?在静态页面中显示图像的第一种方法仍然存在相同的问题,这使我认为它与TemplateView有关。

The second method I found that worked for just displaying the static page (not the image) was to create a new app called landing and have that simply display a static page from the urls.py in the same manner(using TemplateView). Is this method better? I still had the same problems in displaying an image within the static page as the first method, which makes me think it has something to do with the TemplateView.

我在做这完全错了?我有什么选择?我正在使用Django 1.5.1。

Am I doing this completely wrong? What are my options? I am using Django 1.5.1.

提前感谢

推荐答案

p>使用Django提供静态文件有点尴尬。这是因为,由于性能/安全性原因,静态文件(如图像)必须位于另一个域或服务器中。

It is a bit awkward to serve statics files with Django. This is because they have the conception that static files such as images must be in another domain or server for performance/security reasons.

要使用django提供静态内容,请执行以下操作:

To serve static content with django do this:

# urls.py
# add this lines at the end

from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

然后在 settings.py 中定义images / css / js和类似静态内容的目录:

Then in settings.py define the directory where the images/css/js and similar static contents are:

# settings.py

import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    # this assumes your static contents is in <your_project>/static/
    os.path.join(PROJECT_ROOT, 'static/'),
    )

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'

注意:您还可以将图片视为媒体,并将其放在 media / 文件夹中。

Note: You may also consider images as media and place them in your media/ folder.

这应该会获得与您的应用程序一起提供的静态文件。只要引用它们,就可以这样 href =/ static / xxxx.jpg href =/ media / xxxx.jpg

That should get static files served with your app. Just reference them like this href="/static/xxxx.jpg" or href="/media/xxxx.jpg".

希望有帮助!

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

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