I deploy the static settings like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static/"),
]
And I got a static folder in root with some css files, I use the link like:
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}css/station_list.css'">
<img src="{{ STATIC_URL }}images/logo.png">
The css path is correct but the html can't display the css style, and I check the terminal, it says:
Not Found: /raise/station/css/station_list.css'
Not Found: /raise/station/images/logo.png
Why the path is not /static/ but an app root? Thanks.
For calling static files like css or image {% static 'css/style.css' %} in your href attribute and {% load static %} place this in top of your template
Related
I am trying to add CSS styling to my html email to be sent so I used django-inlinecss 0.3.0
In my template I am using the following:
{% load inlinecss %}
{% inlinecss "/css/bootstrap.css" %}
TEXT
{% endinlinecss %}
Here is the complete path of the CSS file:
C:\Users\User\Desktop\Project\static_in_env\css\bootstrap.css
I have tried the following:
{% inlinecss static "css/bootstrap.css" %}
After debugging I found that the reason is due to
[Errno 2] No such file or directory: 'C:\\Users\\User\\Desktop\\static_root\\css\\bootstrap.css'
Here is the files structure:
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
So, how should I fix this error?
Follow the below steps:
1) setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR/"static/", ]
2) urls.py
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)
3) Create Static Folder
create a static folder inside startproject (project_name).
The Folder name should be "static"
4) Add your css file
Inside that static folder create a "css" folder and then add your css file (bootstrap.css)
Once you done all the setup for static file, now we can work with html template.
5) Html Template
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Custom Css -->
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}" />
</head>
</html>
My style.css is placed in appname/static/appname/.
My settings.py has this code:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static/"),
)
And in my base.html I load it like this:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">
But the styles are not loading.
If I remove STATICFILES_DIRS and change STATIC_URL = '/static/' to STATIC_URL = '/static/appname/', it works perfectly, but I guess it's not the best practice for the case I'll add any other app to the project later. What I might be doing wrong?
Just change one thing,
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
It will search in static folder inside your app. Also if you want to add a specific directory,
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), '/your specific directory/',
)
From here you can directly add the particular file name, and djnago will search in that specific directory.
Remove "appname" in {% static 'appname/style.css' %}, you must not place it there because python knows automatically in which application the file is, it get the application name from the request
By default django picks static directory from app's directory. So, if your static directory is inside app directory there is no need to specify STATICFILES_DIRS.
Now /static/ will point to files and directories in the static directory of your app. To refer your css file use
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">
I have created a project in Django. Also, I am using django-allauth for sign up and login.
In order to use my own templates with django-allauth, I have created a html file called signup.html in a folder called account inside a folder called templates which is outside of of all my apps (/templates/account/signup.html). That works.
I tried to use some custom css file inside signup.html:
<link rel="stylesheet" href="/templates/account/signup.css">
It says that the file can not be found. Though, it is located in templates/account.
your css file must under STATICFILES_DIRS in settings.py,set this in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
and put account/signup.css file to /static/account/signup.css,you can get it like:
<link rel="stylesheet" href="/static/account/signup.css">
or
{% load static %}
<link rel="stylesheet" href="{% static 'account/signup.css' %}">
My settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
In my html page is href to my files using
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/default.css">
<img src="{{ STATIC_URL }}images/pythonlogo.jpeg">
I also tried:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/default.css' %}">
<img src="{% static 'images/pythonlogo.jpeg' %}">
The error I am getting in developer tools is
GET http://127.0.0.1:8000/static/css/default.css 404 (NOT FOUND)
GET http://127.0.0.1:8000/static/images/pythonlogo.jpeg 404 (NOT FOUND)
I tried to print the path on to the web page by simply placing {{STATIC_URL}} on the page and /static/ appears.
My project directory path is:
django_test/
admin/
article/ <-- app
templates/
django_test/
templates/
static/
css/
images/
STATIC_ROOT is directory where all your static files will be copied by collectstatic command.
You should specify your path to STATICFILES_DIRS tuple to use it with built in webserver.
I am learning Django and trying to create modular templates and I am coming across this issue
In my Developer tools I am getting this error:
GET http://127.0.0.1:8000/static/assets/css/default.css 404 (NOT FOUND) 127.0.0.1/:8
GET http://127.0.0.1:8000/static/assets/images/pythonlogo.jpeg 404 (NOT FOUND) 127.0.0.1/:84
From my understanding, having STATIC_URL = '/static/' allows {% static %} to be used and also appends /static/ to the path of your static folder. Also, using STATICFILES_DIR is for locating the static files in your project.
Currently I have:
STATIC_URL = '/static/'
STATICFILES_DIR = (
('assets', '/Users/BobDole/Development/django-brad/django_test/'),
)
From reading the documentation, it seems to me that 'assets' is used as a namespace or a variable to represent /Users/BobDole/Development/django-brad/django_test/
In my html page I used
<img src="{% static 'assets/images/pythonlogo.jpeg' %}">
<link rel="stylesheet" type="text/css" href="{% static 'assets/css/default.css' %}">
My current project directory structure
django_test/
admin/
article/ <-- app
templates/
django_test/
templates/
images/
static/
css/
I believe that I am using STATIC_URL and STATICFILES_DIR improperly could some provide me with some suggestions? Thank you!
try this:
<img src="{{ STATIC_URL }}assets/images/pythonlogo.jpeg">
Django will change STATIC_URL for /whatever/whatever/static/, so the url the img will access is: /whatever/whatever/static/assets/images/pythonlogo.jpeg
The thing is /whatever/whatever/static/ has to be the path until the "static" folder inclusive
I use in my projects STATIC_URL like this(settings.py):
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, "static")
STATIC_URL = '/static/'
EDIT You have to add this 3 lines in your settings.py
What makes you think assets is magic in some way? It's not, it's simply the name of a directory. You don't have a directory called that, so you shouldn't use it. Use {% static 'images/pythonlogo.jpeg' %} etc.