10

In django, all urls are parsed from urls.py file. So, there is no directory structure as such.

So, what if you have to include a css file in your template ?

Is there a way without adding it to url.py file ?

If no, then will you make new entry in urls.py for every resource ?

2
  • 1
    Obviously, no.. it was a doubt that came out. I haven't read the documentation to the full yet. Commented Aug 15, 2011 at 12:48
  • 2
    A simple link to the documentation would have done it.. but then you have to down vote always.:( Commented Aug 15, 2011 at 12:57

3 Answers 3

9

See the Django HOWTO on static files.

Basically, in your configuration file, you specify a special directory to store static files in. The example in the docs is:

STATIC_ROOT = "/home/jacob/projects/mysite.com/sitestatic"

You put CSS files, images, etc. in there, and the server will know to serve URLs that match your static URL pattern from that directory.

Sign up to request clarification or add additional context in comments.

3 Comments

Nope: # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/var/www/example.com/static/" STATIC_ROOT Essentially, this is where your static files will go when you run a command like python manage.py collectstatic, but you don't ever put anything in here yourself.
@doctordoder It looks like things have changed since I answered this question two and a half years ago. Feel free to update this answer for modern Django.
Would do so if I knew how. Currently, I'm having trouble doing this myself (I'm working through the guide and looking at old SO threads). I only really know that that won't work, unfortunately. Sorry to anyone else in the same boat as me.
6

You no longer need to specify STATIC_ROOT (for djangor > 1.10). Simply, make sure

django.contrib.staticfiles

is included in INSTALLED_APPS

and

STATIC_URL = '/static/'

in your settings.py

Create a directory called "static" in your app directory, and inside the created static directory, another subdirectory named your app and include the static files there (you could also create js, img, css subdirectories inside the last directory based on your preference if you need)

Then, include the correct path in the template file. For ex:

src = "/static/my_app/example.js"

or

src = "/static/my_app/js/example.js"

(assuming your javascript files are in a directory called js)

Alternatively (much better), define the path using the static template tag:

{% load static %}
<script src="{% static "my_app/js/example.js" %}"></script>

All you need to know:

https://docs.djangoproject.com/en/1.10/howto/static-files/

2 Comments

ah, finally something that actually works! thank you!
I have a shared template that doesn't belongs to a particular app. So I have a template in the project root folder. I did as you suggest: In root I have static folder inside it i have a folder with the name of my app, then I have css/styles.css. Inside the template I did: {% load static %} And call it: href="{% static "flights/css/styles.css" %}" Once the template is shared, I also tried just put static/css/styles.css and call for: href="{% static "css/styles.css" %}" But still not working...
2

In my root urls.py file, I use this pattern to serve static files when developing locally. I also add a setting called SERVE_STATIC_MEDIA so I can separate it from DEBUG.

if settings.SERVE_STATIC_MEDIA:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', \
            {'document_root' : settings.MEDIA_ROOT}),
    )

Then in a template, you can access css, js, images, as such:

<link type="text/css" href="{{ MEDIA_URL }}css/foo.css" media="screen,projection" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.