So I am noticing that my css changes are not being reflected. I did a hard-reload and cleared the cache but still nothing. Like I can literally delete the css file and my badge which I'm trying to edit is still there... it only goes away once I take it off of base.html directly. So what is going on here? I have a static folder in my app, with a css folder and then my css/notification file. I tried doing collectstatic through terminal but that doesn't do anything. Also, I already have my load static tag in my html.
settings.py
STATIC_URL = '/static/'
STATICFILES_DIR = [
"/DatingAppCustom/dating_app/static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'dating_app/media')
settings.py/installed_apps
'django.contrib.staticfiles'
project urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dating_app.urls', namespace= 'dating_app')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
base.html
{% load staticfiles %}
you can use STATICFILES_DIRS instead of STATICFILES_DIR.
also write ALL PATH in STATICFILES_DIR like this.
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Related
I'm trying to load my own customized CSS in Django but with no success. The weird thing is that the main CSS (style.css) is loading correctly.
Here is my base.html:
<!-- Main Style CSS -->
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<!-- My Own Style CSS -->
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
My settings:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
STATIC_DIR,
]
My urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('cart/', include('cart.urls')),
path('payment/', include('payment.urls')),
path('orders/', include('orders.urls')),
# path('users/', include('django.contrib.auth.urls')),
path('', include('django.contrib.auth.urls')),
path('', include('account.urls')),
path('', include('dma.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Any help?
Thank you!
It won't work because you didn't specify a MEDIA_URL, so try this:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_URL = '/css/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
STATIC_DIR,
]
where is located your custom css ? in which static folder ?
in project level static folder next to css/style.css ? in this case it should be loaded with no problem maybe you have a typo in the file name ?
in app static folder ?
in this case, i guess you need to add
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
refer to https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-STATICFILES_FINDERS
Update
maybe you need to re-run the command below:
python manage.py collectstatic
so the new assets can be taken in consideration the next reload.
I want to write Django code into CSS style tag in HTML elements in order to get images from static directory. here is my original code
i tried with the below code
but this isn't worked for me, how can i solve this problem?
NB: i used {% load static %} to load all static files, css and js works fine but i became failed to do this.
First Add These Code In Your settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'myproject/static')
]
# Media settings
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Then Add These Code In Your project urls.py
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Then add this line to your html
style="background-image: url({%static 'images/person_1.jpg' %});"
You can check if in your root project/settings.py has something like this:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
and then, in your root project/urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [...] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
To be sure, make sure you restart your server after these changes.
In settings.py add this
STATIC_URL = 'static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
try to convert style tags in this manner
style ="background-image:url(images/image_1.jpg)"
TO
style ="background-image:url('/static/images/image_1.jpg')"
I have implemented wagtail on my site and when I put it into production it is not finding the css or any images. I have run
python manage.py collectstatic
and all of my css is in a directory called static_files in the same directory as manage.py.
In base.py I have:
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
....
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "static_media")
On my development site BASE_DIR is the directory with static_files, but it does not seem to pick it up on the production site.
Is there any way that I can test to see where the production version is looking for the css, e.g in the HTML?
[Edit]
I have looked at the answers to this question and it does not address the core of my problem which is that certain essential code was not being called when DEBUG is set to False
You need to include the following code in urls.py in your project directory:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
...
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
For some reason this was in an if section under:
if setting.DEBUG = True
and I had DEBUG set to False. If I move it to the main body of the module, it works perfectly
In the references to the static assets in templates, be sure to use the {% static ... %} template tag (reference). In the references to media files in templates, be sure to prefix with {% get_media_prefix %} (reference). Also, I typically use the same final designator for both STATIC_URL and STATIC_ROOT (i.e. /static/, not /static/ for one and static_files for the other). Here is how I declare these variables:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL.replace('/', ''))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, MEDIA_URL.replace('/', ''))
I'm in debug and serving my static file with
STATIC_URL = '/assets/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "assets/"),
os.path.join(BASE_DIR, "assets/css/"),
os.path.join(BASE_DIR, "assets/js/"),
os.path.join(BASE_DIR, "assets/fonts/"),
)
{% load staticfiles %}
{% static ....... %}
Inside my static folder i have:
-css
--bootstrap.min.css
-fonts
--glyphicons-halflings-regular.svg
--glyphicons-halflings-regular.eot
--glyphicons-halflings-regular.ttf
--glyphicons-halflings-regular.woff
-js
In the request I can see that the css (that is loaded correctly) has the following url:
http://127.0.0.1:8000/assets/bootstrap.css
and the font that is referenced in the css (but not loaded) by
url('../fonts/glyphicons-halflings-regular.woff')
has the following url in the request
http://127.0.0.1:8000/fonts/glyphicons-halflings-regular.woff
Is there a way to serve the font files properly without changing there url in the css file ?
I have a problem using css in my django template,
In my settings.py i have this :
BASE_DIR = os.path.abspath(os.path.dirname(__file__) + '/')
STATIC_URL = BASE_DIR + '/static/'
In my paths I have the folder "static/css/home_css.css"
In my template home.html I have the link tag :
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/home_css.css" media="all" />
but it doens't work in order to render de css.
If anybody knows what happens please
STATIC_URL shouldn't point to the path in the filesystem. STATIC_ROOT should.
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = "/static/"
In your settings.py add 'django.core.context_processors.static', at TEMPLATE_CONTEXT_PROCESSORS like in this example:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.request',
'django.contrib.messages.context_processors.messages',
)
EDIT
And if you're working with the local dev-server you'll need something like this in your urls.py:
(r'static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '%s' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')}),
I solved it!
I put my directory static into aplication root path, and set
STATIC_URL = '/static/'
thanks