57π
For Django to serve static files, you have to make sure you have a couple of settings.
STATIC_URL
This setting specifies what URL should static files map to under. You have that done already.
STATICFILES_DIRS
This specifies all the folders on your system where Django should look for static files. The idea is that you might have a couple of apps within your project, and each app might require a different set of static files. So for organizational purposes, each app might contain a static
directory where it will store only its static files. So then Django has to have a way to know where those directories are. This is what this setting is for.
STATIC_ROOT
This setting specifies where Django will copy all the static files to and not where the static files are already at. The idea is that once you leave development into production, Django canβt serve static files anymore due to issues I will not go here (itβs in the article). However, for production, all static files should be in a single directory, instead of in many like specified in STATICFILES_DIRS
. So this setting specifies a directory to which Django will copy all the static files from all files within STATICFILES_DIRS
by running the following command:
$ python manage.py collectstatic
Please note this is only necessary once you go into production and also that the directory specified here cannot be the same as any directory specified in STATICFILES_DIRS
.
Urls.py
In development for Django to serve your static files, you have to include the static URLs in your urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = ...
urlpatterns += staticfiles_urlpatterns()
Once you will complete all of the above things, your static files should be served as long as you have DEBUG = True
. Out of the list above, you seem to only complete STATIC_URL
. Also please note that all the steps I described above are in the docs you linked in your question (link). It might be a bit confusing in the beginning but if you read it a couple of times, it becomes clearer.
14π
Try clearing your cache. If you are using Google chrome go to your settings>clear browsing data> select clear cached images and files then click clear data
- [Django]-Django on IronPython
- [Django]-Django UrlResolver, adding urls at runtime for testing
- [Django]-Detect whether Celery is Available/Running
5π
For me it was changing
<link rel="stylesheet" href=" {% static '/css/style.css' %} ">
to
<link rel="stylesheet" type="text/css" href=" {% static '/css/style.css' %} ">
- [Django]-No module named 'polls.apps.PollsConfigdjango'; Django project tutorial 2
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
- [Django]-How to customize activate_url on django-allauth?
4π
Sometimes all it takes is just "Ctrl + F5"
A Total refresh of the page, does the trick.
Or Ctrl + Shift + R
- [Django]-AngularJS with Django β Conflicting template tags
- [Django]-Django filter many to many field in admin?
- [Django]-How to customize default auth login form in Django?
2π
After doing all, setting DEBUG= True, python collectstatic, clearing cache, opening in incognito mode if the problem still exists copy your .css file into another new .css file in static folder, and then run collectstatic command. This worked out for me.
I hope this will help you.
- [Django]-Convert seconds to hh:mm:ss in Python
- [Django]-How can I allow django admin to set a field to NULL?
- [Django]-Django using get_user_model vs settings.AUTH_USER_MODEL
1π
Adding RequestContext to the response should load the STATIC_URL variable into the template.
Try changing:
from django.shortcuts import render_to_response
def index(request):
return render_to_response('index.html')
to:
from django.shortcuts import render_to_response
from django.template.context import RequestContext
def index(request):
return render_to_response("index.html", context_instance=RequestContext(request))
Refer to the Django Documentation on Referring to static files in templates for more information.
- [Django]-Difference between User.objects.create_user() vs User.objects.create() vs User().save() in django
- [Django]-Django-object-permissions Vs django-guardian Vs django-authority
- [Django]-Composite primary key in django
1π
If there is no problem in coding and no errors shown.
Then you can do this this to try to solve the problem.
Clear your Cache:
If you are using Google chrome go to your settings β> clear browsing data β>
select clear cached images and files then click clear data
- [Django]-Django QuerySet order
- [Django]-Newline in models.TextField() not rendered in template
- [Django]-Parsing unicode input using python json.loads
0π
If this is happening to you in development mode, make sure you set DEBUG=True
in your settings.py
file. Also make sure that the MEDIA_URL
and MEDIA_ROOT
are set in your settings.py
file like so :
MEDIA_URL = '/mymediafolder/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mymediafolder')
And then in your main urls file myapp/urls.py
you must have the following :
from django.conf.urls import url, include
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
#Your url patterns here
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The staticfiles_urlpatterns()
is used to serve static files in development mode.
- [Django]-How can I store an array of strings in a Django model?
- [Django]-Tool for pinpointing circular imports in Python/Django?
- [Django]-Raw_id_fields: How to show a name instead of id?
0π
I had to delete my staticfiles folder. It seemed like there was a similarly named file in it which was being read from or written to and this wasnβt the one my app was pulling from for the site. After I ran βcollectstaticβ again, it re-added the staticfiles folder and contents and is now working and updating properly.
- [Django]-What is the clean way to unittest FileField in django?
- [Django]-Django β get HTML output into a variable
- [Django]-Django, after upgrade: MySQL server has gone away
0π
Nothing worked for me except these
I set these in project settings.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
And then in the url file I did this
from django.urls import re_path
from django.views.static import serve
urlpatterns = [
re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
- [Django]-What does this Django regular expression mean? `?P`
- [Django]-Django Model Field Default Based Off Another Field in Same Model
- [Django]-Django Python rest framework, No 'Access-Control-Allow-Origin' header is present on the requested resource in chrome, works in firefox
-1π
My solution may be silly, but maybe it will help someone. If you copy the line from the internet instead of typing it, make sure to adjust your quotation marks. Worked for me.
<link rel=βstylesheetβ type="text/css" href="{% static 'styles.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'styles.css' %}">
- [Django]-Django: Validate file type of uploaded file
- [Django]-ReactJS with Django β real usage
- [Django]-Using JSON in django template
-5π
There is an easy way if you feel that your CSS isnβt working.
If your project isnβt way too huge then you can just make
the CSS file in the same file as the HTML.
And then run it.That way it will run for example
`
<head>
<meta charset="UTF-8">
<title>Promantus Bot</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
background-color:#FF625F;
}
h1, p {
font-family: sans-serif;
text-align: center;
color: #323330;
font-size: 100px;
}
p {
font-size: 30px;
}
#output, #container {
display: flex;
justify-content: center;
margin-top: 100px;
}
input {
background-color: #eee;
border: none;
font-family: sans-serif;
color: #000;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 30px;
}
</style>
</head>
<body>
<div id="output"></div>
<div id="container">
<input type="text" id="input" value="">
</div>
</body>
</html>
`
It's going to run fine this way.
- [Django]-Django, redirect all non-authenticated users to landing page
- [Django]-Manage.py runserver
- [Django]-Django equivalent for count and group by