2012-06-12 53 views
0

我想在我的网站上显示图像。我一直在浏览Django文档和其他帖子在stackoverflow,但我没有得到这个工作。
我有一个图像名称'under-construction.jpg'。它住在/home/di/mysite/myapp/static/images目录中。如何在Django的网页上显示图像?

我有这样的一个模板:

<img src="{{ STATIC_URL }}images/under_construction.jpg" alt="Hi!" /> 
在我的views.py

我有这样的:

def under_construction(request): 
    return render(request, 'under_construction.html') 

在我的settings.py,我有这样的:

STATIC_ROOT = '' 

# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = '/static/' 

# Additional locations of static files 
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
    '/home/di/mysite/myapp/static', 
    '/home/di/mysite/myapp/static/images', 
) 

我执行./manage.py collectstatic,它把大量的文件放在/home/di/mysite/admin/home/di/mysite/images。我必须做些什么让我的形象出现?

+0

STATIC_ROOT ='/home/di/mysite/myapp' – shiva

+0

http://stackoverflow.com/questions/1179453/django-and-serving-static-files可能的重复 –

+0

http://stackoverflow.com/questions/10792256/show-picture-in-django/10792318可能的重复#10792318 – lciamp

回答

2

所有你需要做的就是编辑settings.py作为以下4点,并在相同的文件夹中创建一个新的文件夹(静态)settings.py在文件夹中位于 静态可以创建另一个文件夹(图像)在它你可以把(under_constructioon。JPG)

  1. STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__), 'static'),)

  2. STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder',)

  3. STATIC_URL = '/static/'

  4. STATIC_ROOT = ''

    你与分组完成后。点你可以写
    <img src="{{ STATIC_URL }}images/under_construction.jpg" alt="Hi!" />

0

我已经解决了这个问题。

STATIC_ROOT = '/path/to/project/static/' 
STATIC_URL = '/static/' 
STATICFILES_DIRS = (
) 

即使您这样做,Django也不会直接获取文件。您需要为您的每个应用程序链接此静态目录。只要到下面的命令Django的应用程序目录运行..

cd /path/to/project/my_proj/my_app 
ln -s /path/to/project/static/ 

这种方法仅在调试国防部。您需要在settings.py文件中设置DEBUG = true。这应该与Django的开发服务器一起工作。

Django不会在生产mod中提供任何静态文件。您需要在生产mod中使用web-server提供静态文件。

的更多信息,可以发现here ..

0

生产设置中设置STATIC_ROOT = /path/to/copy/files/to

有你加入

urlpatterns += patterns('django.contrib.staticfiles.views', url(r'^static/(?P<path>.*)$', 'serve'), 

,或者您也可以做到这一点

from django.contrib.staticfiles.urls import staticfiles_urlpatterns 

# ... the rest of your URLconf here ... 

urlpatterns += staticfiles_urlpatterns() 

当然,尽量不要通过django服务器静态文件,它有一定的开销,而不是配置你的http服务器,假设你有一个http服务器(nginx是相当不错的)。

1

我都面临着同样的问题,显示静态图像。在这方面遭受了很多并花了很多时间。所以想到分享我的设置。

settings.py

STATIC_ROOT = '' 
STATIC_URL = '/static/'  
ADMIN_MEDIA_PREFIX = '/static/admin/'   

import os.path 

STATICFILES_DIRS = (  
    "D:/temp/workspace/offeronline/media", 
    (os.path.join(os.path.dirname(__file__), 'static')), 
) 

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
) 

urls.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
    # ... the rest of your URLconf here ... and at the end of the file add the following line 
urlpatterns += staticfiles_urlpatterns() 

最后添加以下代码模板标签和它的工作

<img src="{{ STATIC_URL }}images/i1.png" />