So for settings up my django static files I added these code in settings.py
STATIC_URL = '/static/'
STATIC_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
So I created A static folder In Base Directory and then I created a folder named css inside of it and I added a file named nav.css into it.
<link rel="stylesheet" href="{% static 'css/nav.css' %}">
But it's not working at all. It gives me error :
Failed to load resource: the server responded with a status of 404 (Not Found)
What I did wrong?
In your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]
Related
I have just started learning DJango and am trying to use css file for my html template. As per the documentation, I have added the STATIC_URL and STATIC_ROOT to my settings.py file as follows:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
BASE_DIR is already set as follows:
BASE_DIR = Path(__file__).resolve().parent.parent
In the template, I am using the css file as follows
{% load static %}
<link rel="stylesheet" href="{% static 'style.css' %}">
and the style.css is present at location DJango_App/static, DJango_App being my project directory with the manage.py file. Still I am getting error
"GET /static/style.css HTTP/1.1" 404 1653
DEBUG is set to True
Directory structure is:
DJango_App
|->DJango_App
|->(settings.py, urls.py, views.py, etc)
|->templates
|->(html templates)
|->static
|->style.css
How do I resolve this?
Update:
Looking at your directory structure above, your static files are not under a Django app. In that case, you should also set STATICFILES_DIRS, see:
https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
Original:
Looks like you did not add the static url handler to urlpatterns.
Serving static files during development require add to urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I am trying to compile my scss file and this is the error I have :
Exception Value: relation "static_precompiler_dependency" does not exist
Here is what is in my html head :
<link rel="stylesheet" type="text/css" href="{% static "css/main.scss"|compile %}">
And here in my setting.py :
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static/css')
I can see the COMPILED folder. But still having that exception error
Seems you haven't migrated after installing it.
https://github.com/andreyfedoseev/django-static-precompiler/blob/master/static_precompiler/models.py
It has a model called Dependency. The model is used for imported scss files.
I've created django project and in settings.py configured all the necessary assets
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = [
"~/<project_name>/static",
]
I have all of my static files based in the folder static in the project directory. Inside this folder I have css, js, images folders accordingly. In my base template (that is inherited by others) called base.html I have template tag {% load staticfiles %}. I can't figure out why the page still loads without any static attached, because everything seems to be made properly.
Change to this (more in STATICFILES_DIRS setting):
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
And inside your templates fetch static files like this (Django 1.10):
{% load static %}
<link rel="stylesheet" href="{% static 'css/css-file.css' %}">
Django is not loading my static files. However it is loading my templates which are in the static folder. Also chrome is not seeing the static files either I'm not even getting a 404 error and yes they are linked in the html...but they are not showing up in the network panel
Heres my settings.py file
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS =(
os.path.join(BASE_DIR, 'static'),
)
Here's my html
<head>
<title>MySite | Home</title>
<meta charset="UTF-8">
<link rel="stylesheet" type='text/css' src='css/normalize.css'>
<link href='http://fonts.googleapis.com/css?family=Questrial|Josefin+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" src='css/main.css'>
<script src="https://maps.googleapis.com/maps/api/js"></script>
</head>
Sorry I know this question has been asked multiple times and i've tried all those solutions with no luck. I've spent 2 days trying to figure this out
The approach I take with static files is basically what's outlined in the docs.
In local development, Django will serve static files automatically from the directory specified in STATIC_ROOT as long as django.contrib.staticfiles is in your INSTALLED_APPS and DEBUG = True
My project structure typically looks like this:
my_project/
some_app/
lib/
static/ <------- STATIC_ROOT - used in production. `collectstatic` collects here
static_assets/ <- STATICFILES_DIRS - used in local dev
css/
less/
js/
images/
templates/ <---- TEMPLATE_DIRS
manage.py
settings.py is typically:
INSTALLED_APPS = (
. . .
'django.contrib.staticfiles',
. . .
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_assets'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
Then in templates, you can again use the staticfiles app's template tags to build out paths to static files:
{% load static from staticfiles %}
<link rel="stylesheet" href="{% static 'css/normalize.css' %}" />
Also note that with <link> tags, you need to use the href property for the url instead of src.
In your setting.py file add this
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
create a folder in your app named static and in your template file add this {% load static %}
I have spent the last few days figuring out how to include an css file into a Django template. I still did not succeed so am hoping someone can help me out.
I have the following settings:
--settings.py--
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
I have not set anything in STATICFILES_DIRS() either.
--urls.py--
urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += staticfiles_urlpatterns()
--views.py--
def my_homepage_view(request):
return render_to_response('test.html', context_instance=RequestContext(request))
--test.html template--
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css"/>
--source code localhost--
<link rel="stylesheet" type="text/css" href="/static/css/style.css"/>
According to the Django documentation it seems that I have set everything correctly, but the css style is still not applied. The static folder is in the correct place (C:reviews/reviewsite/static) where the rest of my apps also reside. Even if I hardcode the style.css location (C:reviews/reviewsite/static/css/style.css) in the test.html template the css style is not applied. I have checked the style.css and it works without Django.
Any idea of what I am doing wrong?
This is how you call the files in the static
{% static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}"/>
settings.py
import os
import sys
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
//create staticfiles folder
os.path.join(SITE_ROOT, 'staticfiles'),
)
Everyone, thanks for your help and sorry for the late reply. I tried your suggestions but it didn't work unfortunately. However, after some more time trying I got it working now. This is what worked for me:
--settings.py--
MEDIA_ROOT = ''
MEDIA_ROOT = ''
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = 'http://localhost:8000/static/'
--urls.py--
urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT }), )
I now have placed the css file in the static folder in my apps directory. In the template I am using {{ STATIC_URL }}/style.css.