I am trying to load my CSS, images, and javascript into my Django template using {{ STATIC_URL }}
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
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:
<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:
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:
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.
Here is an example of the correct usage of 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 :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.