Reputation: 665
Code snippet managed static files in settings.py as follows.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = [
os.path.join(
os.path.dirname(__file__),
'static',
),
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR + '/templates/'
],
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'loaders': [
('django.template.loaders.cached.Loader', [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]),
],
'debug' : True
},
},
]
STATIC_URL = '/static/'
url.pyfile is as follows.
from django.conf.urls import include, url
from django.contrib import admin
from tweets.views import Index, Profile
admin.autodiscover()
urlpatterns = [
url(r'^$', Index.as_view()),
url(r'^admin/', include(admin.site.urls)),
url(r'^user/(\w+)/$', Profile.as_view()),
]
views.pyfile is as follows
{% load staticfiles %}
<html>
<head>
<title>Django</title>
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}" media="screen">
</head>
<body>
{% block content %}
<h1 class="text-info">Hello {{name}}!</h1>
{% endblock %}
<script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
</body>
</html>
When I run server, browser can't find bootstrap.min.js file.
Page not found at /static/bootstrap/css/bootstrap.min.css http://localhost:8000/static/bootstrap/css/bootstrap.min.css 'bootstrap/css/bootstrap.min.css' could not be found
It seems that the static files url is correct. But browser can't find the static files. Please tell me the reason and how can I handle it?
Upvotes: 1
Views: 132
Reputation: 344
To serve the static files during development you should add to your urls.py the following code:
from django.conf.urls import include, url
from django.contrib import admin
from tweets.views import Index, Profile
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
url(r'^$', Index.as_view()),
url(r'^admin/', include(admin.site.urls)),
url(r'^user/(\w+)/$', Profile.as_view()),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
And you should create the path in your project folder static/your_app/your_files
Upvotes: 3