Skip to content

Instantly share code, notes, and snippets.

@marcesher
Last active December 10, 2015 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcesher/4510306 to your computer and use it in GitHub Desktop.
Save marcesher/4510306 to your computer and use it in GitHub Desktop.
Notes and questions about static assets in django
my notes on django static assets during development. This is not related to collectstatic or configuring the webserver:
1) created new /static/ directory under 'mysite'
2) put some stuff in there... css, images
How to include in templates:
1) Using the {{STATIC_URL}} seting
--requires Zero additional configuration unlike {% static "blah.css" %}
The only way this works is if the view has the RequestContext injected into it. You'd never know how to do this by reading the tutorial, though.
Two ways:
views.py...
def index(request):
#return render_to_response('index.html', {'message':'Yo Dawg'}, RequestContext(request))
OR
def index(request):
return render(request, 'index.html', {'message':'Yo dawg'})
render() automatically injects the RequestContext.
QUESTIONS:
1) Why is render_to_response used in the tutorial and not render()? Is there some performance implication? If so, is it real or only applicable when building stock exchanges?
2) How in the world is a n00b supposed to know this?
3) If render() is preferred, I'd like to update the documentation to use it instead and to clarify the use of static assets. Seems to me that now you can do it in 2 ways, and there's no guidance on when one way is preferable
4) The docs mention TEMPLATE_CONTEXT_PROCESSORS, but that setting doesn't exist in my settings.py file. Should it? It appears that those processors are not included but presumably enabled by default at a higher level. If that's the case, the docs should clearly state this.
2) Using the {% static "myfile.css" %} template tag
In settings.py:
import os
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'static'),
)
then in base.html:
{% load static %}
...
<img src="{% static "images/logo.png" %}" alt="Logo" />
QUESTIONS:
1) The docs say this is "a more powerful way". My question: is it then also the preferred way? If it's the preferred way, why is it not mentioned first? The "how" here is simple... I need to know the when and why
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment