Django {{STATIC_URL}} sometimes does not work

The pathetic beginner django has a problem with a blocker for accessing static files (css, js) for some kinds. Basically, on the home page, these static files are completely accessible, but on the other page it is not, and the layout is completely broken.

These are my .py settings:

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 template with the extension:

 <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 rendering, it returns the following 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 homepage view code (the one that static files work with):

 def slider(request): context = Context ({ 'articles': Article.objects.order_by('-created_at')[:5] }) return render(request, 'articles/slider.html', context) 

and this is a view that does not access static files (uses the django docs pagination example):

 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 realizes what might be wrong, let me know.

The code for the current project is available as github repo .

+4
source share
1 answer

You should use 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, this is similar to render_to_response , but uses RequestContext :

 from django.shortcuts import render def archive(request, page="1"): # ... return render('articles/archive.html', context) 

Notice that you used render in your slider view.

+7
source

All Articles