Django不显示图像 [英] django not displaying image

查看:127
本文介绍了Django不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从django应用开始,尝试在主页上显示图像.但是没有显示图像.

I started with a django app and tried displaying an image in the home page. But the image is not getting displayed.

我的settings.py:

My settings.py:

MEDIA_ROOT = os.path.join(BASE_DIR,"media")
MEDIA_URL = "/media/"

我的urls.py:

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myapp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),
url(r'',include('users.urls')),
) 

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我的home.html:

My home.html:

<html>
    <head>
            <title>MY SITE</title>
    </head>
    <body>
            <img src="/media/image/myimage.jpg" alt="my image"/>
            Welcome to my site
    </body> 
</html>

我显示的是文本my image而不是图像

I get the text my image displayed instead of the image

推荐答案

好,所以我为此设置了一个简单的成对"工作示例,因此没有丢失任何可动部分.一些注意事项:

ok, so I set up a simple 'paired-down' working example of this so no moving pieces were missing. Some Notes:

  • I used django 1.6
  • If you want to use a 'scale-able' delivery of static images you will want to follow the django-1.6 staticfiles documentation which is what this example provides.

这意味着您需要用hack-n-斜线表示您的MEDIA参考. (MEDIA确实是用于文件上传的),并且您无需弄混urls.py文件中的staticfiles_urlpatterns.当您将'django.contrib.staticfiles'添加到settings.py中时,开箱即用的行为.

This means that you need to hack-n-slash your MEDIA references. ( MEDIA is really meant for file upload's) and you dont need to fool with the staticfiles_urlpatterns in your urls.py file. As you get this behavior out-of-the box when you add 'django.contrib.staticfiles' to your settings.py

概述:这是您的文件树布局在django 1.6中的外观以及我在此答案中引用的文件结构.

Overview: Here is what your file tree-layout will look like in django 1.6, and the file structure that I am referencing in this answer.

├── djproj
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── manage.py
└─── myapp
    ├── admin.py
    ├── __init__.py
    ├── models.py
    ├── static
    │   └── myapp
    │       └── myimage.jpg
    ├── templates
    │   └── index.html
    ├── tests.py
    └── views.py

步骤1:确保INSTALLED_APPS中包含django.contrib.staticfiles.

Step 1: Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

步骤2:在您的设置文件中,定义STATIC_URL,例如:

Step 2: In your settings file, define STATIC_URL, for example:

STATIC_URL = '/static/'

第3步:更改您的home.html使其看起来像这样(我在示例中使用了index.html)

Step 3: change your home.html to look like this ( I used index.html in my example )

[PROJECT_HOME/myapp/templates/index.html]

[ PROJECT_HOME/myapp/templates/index.html ]

<html>
<head>
        <title>MY SITE</title>
</head>
<body>
    {% load staticfiles %}
    <img src="{% static "myapp/myimage.jpg" %}" alt="My image"/>
        Welcome to my site
</body> 
</html>

请确保阅读与 django-1.6静态文件文档相关的内容到STATICFILES_STORAGE,这样您就可以了解这些模板标签在购买您的商品.提示:python manage.py collectstatic

Make sure to read the django-1.6 staticfiles documentation related to STATICFILES_STORAGE so you understand what these template tags are buying you. Hint: python manage.py collectstatic

第4步:(您可能已经做过,但我没有看到)确保TEMPLETE_DIRS在settings.py中定义,并包含home.html(关键是django的模板引擎需要为此提供服务,因为我们将使用一些模板标签)

Step 4: ( you may have already done this, but I did not see it) Make sure that TEMPLETE_DIRS is defined in your settings.py and includes your home.html ( key here is that django's template engine needs to be serving this as we will be using some template tags)

[PROJECT_HOME/djproj/settings.py]

[ PROJECT_HOME/djproj/settings.py ]

TEMPLATE_DIRS = (
   # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
   # Always use forward slashes, even on Windows.
   # Don't forget to use absolute paths, not relative paths.
   BASE_DIR + '../myapp/templates',
)

第5步:构建一个可渲染home.html页面的视图.

Step 5: build a view that renders your home.html page.

[PROJECT_HOME/myapp/views.py]

[ PROJECT_HOME/myapp/views.py ]

from django.shortcuts import render_to_response

def index( request ):
   return render_to_response('index.html')

由于这有点令人费解,所以这里有几个动人之处:这里是一个变更集,其中显示了完整的变更"

Since this is a bit convoluted, and there are several moving pieces here: here is a changeset that shows the complete 'changes'

如果其中任何一个令人困惑,您可以粗拉整个github项目. django-staticfiles-setupexample

You can of-coarse pull the entire github project if any of this is confusing. django-staticfiles-setupexample

最终说明:并非总是在django中以这种方式处理STATIC文件. MEDIA目录曾经是您所追求的设置attr.因此,此答案适用于Django 1.6(在django版本1.3中添加了静态文件)(在django 1.5中更改了目录布局).

Final Note: STATIC files were not always handled this way in django. The MEDIA dir used to be the settings attr you were after. So this answer is for Django 1.6 ( staticfiles were added in django version 1.3)( the directory layout was changed in django 1.5).

您可以(正如您尝试的那样)将媒体目录硬连接到STATIC定义.我不建议这样做.这就是为什么我没有描述这种方式的原因.

You can ( as you were trying to do) hardwire your media dirs to the STATIC defines. Altho I do-not recommend this. Which is why I did not describe how to do it this way.

这篇关于Django不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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