Reputation: 26437
A pathetic django beginner has got a blocker problem to access static files (css, js) on some views. Mainly, on the homepage those static files are perfectly accessible, but on a different page, it doesn't and the layout is totally broken.
This is my settings.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'bands',
'lyrics',
'articles',
)
PROJECT_DIR = os.path.dirname(__file__)
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, "static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
This is part of my base generic template which views extend:
<link rel="Shortcut icon" href="{{ STATIC_URL }}img/favicon32.png" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/bootstrap.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}js/sortable.js"></script>
When rendered, it return such code (and this is what I want):
<link rel="Shortcut icon" href="/static/img/favicon32.png" />
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="/static/js/sortable.js"></script>
This is the code of the homepage view (the one that static files work):
def slider(request):
context = Context ({ 'articles': Article.objects.order_by('-created_at')[:5] })
return render(request, 'articles/slider.html', context)
and this is the view that doesn't access static files (it uses pagination example found on django docs):
def archive(request, page="1"):
articles_list = Article.objects.all().filter(active=True)
paginator = Paginator(articles_list, 6)
try:
articles = paginator.page(page)
except PageNotAnInteger:
articles = paginator.page(1)
except EmptyPage:
articles = paginator.page(paginator.num_pages)
context = Context({
'articles': articles
})
return render_to_response('articles/archive.html', context)
If anyone has a guess what might be wrong, please let me know.
The code of current project is available as a github repo.
Upvotes: 4
Views: 788
Reputation: 1
[1] Add your directory name to INSTALLED_APPS in which the static directory is located.
[2] load static in your html template
[1] For Example [I have static in TestAPP] [Check out this link] : https://i.sstatic.net/zHEck.png
[2] Then [load static files in your HTML Template as following] : https://i.sstatic.net/vPkFb.png
Upvotes: 0
Reputation: 62868
You must use the RequestContext
:
from django.template import RequestContext
def archive(request, page="1"):
# ...
return render_to_response('articles/archive.html',
context,
context_instance=RequestContext(request))
Or use the render
shortcut, it is like render_to_response
, but uses RequestContext
:
from django.shortcuts import render
def archive(request, page="1"):
# ...
return render('articles/archive.html', context)
Note you used render
in your slider
view.
Upvotes: 7