2
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/main.css' %}" type="text/css" />

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = (
)

Above are my settings and html settings. Although I can get /static/css/main.css in my HTML file, but I can't load 127.0.0.1/static/css/main.css. What can I do to fix this?

4
  • try {{STATIC_URL}}css/main.css instead. Commented Jul 29, 2013 at 4:37
  • No, I can get "static/css/main.css" in my HTML file, but if I load the address directly from the browser, it simply doesn't work... Commented Jul 29, 2013 at 4:43
  • Can you paste the structure of your project please? Commented Jul 29, 2013 at 4:45
  • 1
    Did you forget the port? 127.0.0.1:8000/static/css/main.css Commented Jul 29, 2013 at 5:14

3 Answers 3

3

To load static successfully you should consider:

  • STATICFILES_DIRS should contain list of folders to your static
  • STATIC_ROOT shouldn't be empty. Set it i.e. collected_static.
  • In case you launch your django site in DEBUG=True django will use static directly from STATICFILES_DIRS and in case you launched django site in DEBUG=False you should make python manage.py collectstatic
Sign up to request clarification or add additional context in comments.

Comments

0

Add line in settings.py

STATIC_URL = '/static/'


MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

run command:- python manage.py collectstatic

static folder generated

enter image description here

FOR LOAD CSS FILES

{% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'media/style.css' %}"/>

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read this how-to-answer for providing quality answer.
0

ON your STATICFILES_DIR = ( )

change it to a list:

STATICFILES_DIR = [
# place STATIC_DIR in here ending in a ,
]

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.