I am trying serve static files in my local development (on same server I serve my site). I have run ./manage.py collectstatic with the following configs:
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
template.html
{% load static %}
<img src="{% static 'apis/icons/random-svgrepo-com.svg' %}" style="width: 50px;">
Now I have staticfiles folder in my BASE_DIR folder, but when I try to get an image, it doesn't appear to be working expected.
http://127.0.0.1:8000/static/apis/icons/random-svgrepo-com.svg
Please help. What I am doing wrong?
First of all I think you have not configured your static files and media files. Try configuring it as follows. In your settings.py ensure to include STATICFILES_DIR, STATIC_ROOT, MEDIA_URL, MEDIA_ROOT in your settings.py and then add the below lines below STATIC_URL = 'static/':
STATIC_URL = 'static/'
MEDIA_URL = 'media/'
STATICFILES_DIRS = [BASE_DIR / 'staticfiles']
STATIC_ROOT = BASE_DIR / 'staticfiles/'
MEDIA_ROOT = BASE_DIR / 'static/media'
By doing this you are telling django where to get your static files. now your have to add the link of the static files in your project urls.py. you will add that as below.
from django.conf import settings
from django.conf.urls.static import
static
urlpatterns = [
.......
]
urlpatterns +=static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
One last thing that I assumed you had done is creating static folder in your project root and inside that static folder create media folder where all the images you want to load are. Now run python manage.py collectstatic
Related
I have tested my app in development and successfully got it fully functional in development running on my local server. I have now been trying to push it into production and none of the static files are being served. The connection has been successful as the domain shows the app with just plain HTML. However, all static files are returning the same 404 error.
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))`
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
EDIT: installing whitenoise fixed this!
try this :
urls.py
from django.conf.urls.static import static
urlpatterns = [
...
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Or just try this command:
python manage.py collectstatic
try this on top every HTML file if you didn't:
{% load static %}
this is because when you add some static files then you must tell manually the template to load those static file.
for more you can see here
Here is the configuration of my static_root variable:
STATIC_ROOT = [os.path.join(BASE_DIR,'vol/web/static')]
My templates are at the root of the project.
TEMPLATES={
...
'DIRS':[os.path.join(BASE_DIR, 'templates')],
...
}
This is how I call my bootstrap.min.css file in a project template:
<link rel="stylesheet" href="{% static 'assets/plugins/bootstrap/css/bootstrap.min.css' %}">
But it doesn't work because the file is not loaded. What's the right way to do it?
Inside main urls.py
from django.conf import settings as SETTINGS
from django.conf.urls.static import static
urlpatterns += static(SETTINGS.STATIC_URL, document_root=SETTINGS.STATIC_ROOT)
urlpatterns += static(SETTINGS.MEDIA_URL, document_root=SETTINGS.MEDIA_ROOT)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
STATIC_URL = '/static/'
STATICFILES_DIRS = (BASE_DIR / 'static',)
STATIC_ROOT = (BASE_DIR / 'static'/ 'static')
If you store your js and css files in some folder, then you need to specify this folder in STATICFILES_DIRS django setting. For example:
STATICFILES_DIRS = [
"/home/my-user/my-project/static",
]
Then Django will search this directory(ies) when looking up files specified in {% static ... %} tag.
https://docs.djangoproject.com/en/4.0/ref/settings/#staticfiles-dirs
If Django still unable to find particular file, try using findstatic management command for diagnostics. This will show in which directories django is actually looking for the file. For example:
python manage.py findstatic -v3 assets/plugins/bootstrap/css/bootstrap.min.css
https://docs.djangoproject.com/en/4.0/ref/contrib/staticfiles/#findstatic
I'm starting to configure my first Django project and I find this issue which is really bothering me.
I have set a root static folder to put some css files for my base template there, but Django is not finding any css files there.
My settings.py are like this:
...
BASE_DIR = Path(__file__).resolve().parent.parent
...
STATIC_URL = '/static/'
SATICFILES_DIRS = [
BASE_DIR / 'static',
BASE_DIR / 'sales' / 'static'
]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
...
...
And in my urls.py I have:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('sales.urls', namespace='sales')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT, show_indexes=True)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT, show_indexes=True)
...
If a run findstatic I get the following:
$ python manage.py findstatic --verbosity 2 static
No matching file found for 'static'.
Looking in the following locations:
/home/dancab/git/django-3-course/myenv/lib/python3.9/site-packages/django/contrib/admin/static
/home/dancab/git/django-3-course/src/sales/static
And also, in the browser, I can see the list of files in the MEDIA folder, but I can't see the STATIC folder, I get the following error:
I don't understand why I Django finds the MEDIA folder and not the STATIC folder.
Thanks in advance to anyone that gives me a hint on why this happens.
before the line STATIC_URL = '/static/', set static root like
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'project_folder/static') # if not works, set actual path
]
don't forget to place your project folder name.
Then run
python manage.py collectstatic
Then you can load static using jinja expression in your html files like
{% load static %}
Then you can link your static css files in root->static->css folder like
<link rel="stylesheet" href="{% static 'css/your.css' %}">
I'm working on a Django project and I can't see my static folder, I'm also having a problem displaying images.
And when I inspect the image div in the src it's written unknown, Here's how I display the image from the admin
``<img class="rounded-circle account-profile" src="{{ user.Profile.profile_photo.url }}" alt="profile">``
User is the current user, the profile and then the name of the column.
My folder structure is route-folder( project-folder, media-folder and app-folder )
My static settings
```STATIC_URL = '/static/'
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = [
os.path.join(SITE_ROOT, "static/"),
]```
The Url:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I think you are trying to retrieve a media image not a static one. I recomend using Whitenoise package to serve static files.
Follow instruction in this tutorial: http://whitenoise.evans.io/en/stable/django.html
Check you have all this setup:
# Settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
In the templates, you should remember to add the {% load static %} to grant access to the actual files.
When I load my local site I cannot get the CSS files to load. I'm running Django 1.9 and python 3.4.2.
Here is my structure:
apps/
app1/
app2/
etc.
clients/
media/ #css, js, images, etc.
static/ #static files
templates/ #html templates
__init__.py
manage.py
settings.py
etc.
In my settings.py file I have:
STATIC_ROOT = os.path.join(BASE_DIR, 'clients', 'static')
STATIC_URL = 'clients/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'clients', 'media'),
]
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'clients', 'media')
And my html template that is calling the css files is as so:
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/primary_stylesheet.css' %}" />
I continue to get a 404 error that says it can't find the css file in:/clients/static/css/primary_stylesheet.css
In my settings.py file I have printed out my STATICFILES_DIRS and STATIC_ROOT and they both lead directly where they should. I've read through the documentation and tried multiple variations of DIRS and ROOT and don't understand why the css file is not pulling correctly - even "collectstatic" is collecting correctly.
I greatly appreciate any help and wisdom someone else has to give.
Thank you!
If you have DEBUG = True set then django won't actually pull your files from the /static/ folder - it finds and collects your staticfiles at runtime when you input the runserver command.
I think you'll find that if you use the default setting for STATICFILES_FINDERS your app will be able to serve your files:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
If you are running your server with python ./manage.py runserver, you need to set urls for both static and media files, check here: https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development
When I am starting a new project, I generally set my urls.py like this:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
url_patterns = [
url(r'^admin/', admin.site.urls),
# your url patterns go here
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This requires you to set STATIC_ROOT, STATIC_URL, MEDIA_ROOT and MEDIA_URL in your settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_files'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'