简体   繁体   English

将静态文件加载到Django中

[英]Loading static files into Django

I am trying to load my CSS, images, and javascript into my Django template using {{ STATIC_URL }} 我正在尝试使用{{ STATIC_URL }} CSS,图像和javascript加载到Django模板中

I am having a problem getting it to work. 我在使其工作时遇到问题。 Here is the relevant code: 以下是相关代码:

Url's.py: 网址:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
urlpatterns += staticfiles_urlpatterns()

Settings.py Settings.py

import os
DEBUG = True
TEMPLATE_DEBUG = DEBUG

PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))

MEDIA_URL = '/assets/'

STATIC_ROOT = os.path.realpath(os.path.join(PROJECT_ROOT, 'static'))

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    "/Users/Chris/project/static/",
)

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

Stylesheet URl: 样式表URl:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css">

If you need anymore information just ask, and thanks for your help! 如果您需要更多信息,请询问,并感谢您的帮助!

UPDATE: I have added the following things trying to fix the problem. 更新:我添加了以下内容来尝试解决此问题。 But it still persists: 但它仍然存在:

To the views where I'm trying to import the stylesheet: 到我要导入样式表的视图:

'context_instance':RequestContext(request),

To the setting.py file: 到setting.py文件:

TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",

)

Here is my Installed App's if this helps: 如果有帮助,这是我的已安装应用程序:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'djblog',

)

My URLs.py file for the admin, and pointing to my other URL file: 我的admins的URLs.py文件,并指向其他URL文件:

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

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

)

My main URL file: 我的主要网址文件:

from django.conf.urls.defaults import *
from views import *
from models import *
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = patterns('',
(r'^$', list),
(r'^archive/(?P<archive>\d{1,2})/$', list),
(r'^\d{4}/\d{1,2}/(?P<sl>.*)/$', detail),
(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/$', month),
(r'^(?P<year>\d{4})/$', year),
(r'^category/$', category),
(r'^category/(?P<category>.*)/$', one_category),
(r'^tag/$', tag),
(r'^tag/(?P<tag>.*)/$', one_tag),
)
urlpatterns += staticfiles_urlpatterns()

I have solved my own question. 我已经解决了我自己的问题。 With the help of Kay Zhu 的帮助下凯朱

In my views I was passing the "context_instance" keyword argument to render_to_response, wrong. 在我看来,我将“ context_instance”关键字参数传递给render_to_response,这是错误的。

Here is an example of the correct usage of context_instance: 这是正确使用context_instance的示例:

def one_tag(request, tag):
    #http://site_name/tag/tag_name
    posts = Post.objects.order_by('-published').filter(tags__name=tag.lower())
    return render_to_response('blog/one_tag.html', 
                              {'posts':posts, 'tag':tag,},
                              context_instance=RequestContext(request))

This might also solve your similar problem. 这也可能解决您的类似问题。 Add a file to your program directory with the following code: 使用以下代码将文件添加到您的程序目录:

def media_url(request):
    from django.conf import settings
    return {'media_url': settings.MEDIA_URL}

And this blog post has a lot of good information: http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/ Be wary though, its from 2006 :) 而且此博客文章提供了很多很好的信息: http ://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/不过请注意,它来自2006年:)

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

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