简体   繁体   中英

Django can not find the css file specified in html file

In my Django project, I have a html file :

<link href="{{ STATIC_URL }}bootstrap/bootstrap.css" rel="stylesheet" type="text/css" media="screen">

And STATIC_URL = '/static/' in settings.py. My urls.py is:

urlpatterns = patterns('',
url(r'^$', WelcomeView.as_view(), name='welcome'),
)

My Django project can NOT FIND the bootstrap.css file in /static/ directory. Should I add a url in urls.py ? And what should I write this url?

When my html file is:

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

The brower says " TemplateSyntaxError at / Could not parse the remainder: 'bootstrap/bootstrap.css' from 'bootstrap/bootstrap.css' "

My settings.py is:

    import os
import sys

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)


ROOT_PATH = os.path.dirname(os.path.abspath(__file__))

if ROOT_PATH not in sys.path:
    sys.path.append(ROOT_PATH)

STATIC_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '..', 'static'))
#STATIC_ROOT = /home/mysite/static

TEMPLATE_ROOT = os.path.join(ROOT_PATH, 'templates')

STATIC_URL = '/static/'
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mysite',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

Do you directory looks like below:

-youproject
|___static
|   |___ bootstrap
|       |___bootstrap.css
|___templates
|   |___yourhtml.html
|___vies.py
|___models.py

My django project is just like that.So,check your project's directory.

wish can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM