Static file collections - Logo navbar does not show up - django

I am trying to have an image (logo) on the navbar.
My project is called "mysite-project" (where manage-pyis), it contains the app "mysite".
In order to upload my static file I did the following:
1) mysite-project/mysite/settings.py
I added:
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/')
]
2) Created folders static and added my logo.png in:
mysite-project/static/mysite-project/logo.png
3) mysite-project/templates/base.html
{% load staticfiles %}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url 'home' %}">
<img src="{% static 'mysite/logo.png' %}" height=30 width=30 class="d-inline-block alighn-top" />
Code of Conduct
</a>
</nav>
4) In mysite-project/mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('users/', include('django.contrib.auth.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
HOWEVER the image does not show up. I think I have some issues in the settings.py for the folders but I cannot find where

The issue is in your STATICFILES_DIRS setting. If you join a path that has a leading slash then the result will "ignore" any preceding arguments and everything after will be relative to root
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/') # This will result in "/static/"
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static') # This will result in "<BASE_DIR>/static/"
]
STATIC_ROOT is the directory that you want to serve files from and the directory that collect_static will populate with all your static files, STATICFILES_DIRS is where Django will collect the files from. STATICFILES_DIRS shouldn't contain STATIC_ROOT. The usual layout for a project is something like this
myproject/ # The root of your repo
myproject/
myapp/
static/ # This is where you put app specific assets
...
static/ # This is where you put your generic static assets. Add this to STATICFILES_DIRS
...
static/ # This is STATIC_ROOT and where your files are served from after being collected
The default value for STATICFILES_FINDERS will look in STATICFILES_DIRS and every apps static directory. If you are using git, you should add the static folder at the root of your repo to .gitignore

Related

my static folder where I place my js ans css files is not at the root of my django project. How to call these satic files in the template?

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

Django. not able to access staticfiles in local development

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

Django image not showing up in template (not URLs issue)

I've added the neccessary elements to the URLs.py.
The image is NOT stored in the database, it's just a background image.
The source of the img folder is 'storiesapp/static/storiesapp/img'
Settings.py (i've been trying different things as shown)
STATIC_URL = '/storiesapp/static/storiesapp/'
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = [
"/storiesapp/static/storiesapp/img/",
"/static/storiesapp/img/",
"/storiesapp/img/",
"/img/",
]
STATICFILES_FINDERS =[
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# full path where all images are stored - os.path will create the directory regardless of the operating system
MEDIA_URL = '/media/'
#
.URLs.py
from django.conf import settings
from django.conf.urls.static import static
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
.
I'm trying all of the following
<link rel="apple-touch-icon" sizes="76x76" href="./assets/img/image.png">
<img src="static/storiesapp/img/image.png">
<img class='img-responsive' src="{{ MEDIA.URL }}{{ image.png }}" />
The answer is "<img src="{% static 'storiesapp/img/image.png' %}"/>".
It was the wrong syntax.
I was able to come to this solution because I realized that my CSS templates, which I can see are working, are also using the static folder so it couldn't be a problem with the way anything is set up.

How to manage static files from Django 1.8 to Django 1.10

I have troubles with upgrading my project from Django 1.8 to Django 1.10: static files are not loaded anymore.
My template looks like this:
{% load staticfiles %}
<!DOCTYPE html>
...
<link href="{%static 'file.css' %}" rel="stylesheet">
...
Then in my file settings.py, I have 'django.contrib.staticfiles' as an installed app. DEBUG is set to True, and I have:
STATIC_URL = os.path.join(BASE_DIR, 'static/')
STATIC_ROOT= os.path.join(BASE_DIR,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), "./", ]
But when the html is produced, it is like the %static has no effect anymore. It is replaced by the empty string (the same works fine with Django 1.8, where the %static is replaced by the content of STATIC_URL). Does anyone know how to fix this ?
Can you added the Update the urls.py(mainproject/urls.py), once you made the chnages run the python manage.py collectstatic command.
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
===================
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
def root(folder):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',folder)
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
root('static'),
)

Django Will Not Pull CSS Static File (404 Error) Even Though File Paths Look Correct

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/'