Static file not found - python

I am using Saleor and I am trying to add a new static folder called fonts into the static folder. When I add a file to static/images I can reference the file, but if I create a folder called fonts: static/fonts and add the same file to it, the file is not found in the browser.
I have tried "npm run build-assets" and cleared Cached images and files in the browser but the font file is still not found.
Works:
<link href="{% static 'images/font-awesome.css' %}" rel="stylesheet" type="text/css">
Does not work:
<link href="{% static 'fonts/font-awesome.css' %}" rel="stylesheet" type="text/css">
Any idea how to make new folders load into static files?
BTW I feel like this must be a nodejs thing.

Here, i really didn't know this package but it's look like you have to add your folder to STATICFILES_DIRS :
https://github.com/mirumee/saleor/blob/221b101a4517d9e4b409523fcb81649be368af95/saleor/settings.py#L81
So it should look like that i think :
STATICFILES_DIRS = [
('assets', os.path.join(PROJECT_ROOT, 'saleor', 'static', 'assets')),
('images', os.path.join(PROJECT_ROOT, 'saleor', 'static', 'images')),
('dashboard', os.path.join(PROJECT_ROOT, 'saleor', 'static', 'dashboard')),
('fonts' os.path.join(PROJECT_ROOT, 'saleor', 'static', 'fonts'))
]
Have fun !

Related

Django only serving a single static file

I just got started in Django a few days ago from Ruby and have only run into one really annoying problem that I just can't seem to figure out on my own. I've tried everything I can think of to no avail.
I am trying to serve up two static files, a custom CSS file (style.css) and a bootstrap.min.css file.
While this should be very easy as everybody keeps telling me, I must be staring at the outside of the box because I can't fix it. I would like to note, it does not work in both live (which I don't expect it too because I don't have a root set) or local environments. It currently will only serve up bootstrap.min.css
EDIT:
When collectstatic is run, it passes through. I have set STATIC_ROOT to 'STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
my settings file:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
My Main Template:
<head>
<meta charset="utf-8">
<title>{% block title %}Django Boards{% endblock %}</title>
<link href="https://fonts.googleapis.com/css?family=Peralta" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
My File Structure:
Main Project
boards
project1
static
css
bootstrap.min.css (loads first files)
style.css (wont load second file)
templates
manage.py
db.sqlite3

Not able to load static files

I tried fetching static files for my website but nothing seems to work even the other stackoverflow answers.
please help on this.
settings.py -
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static','static_root')
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static','static_dirs')
]
File is present in :
parent_folder>static>static_dirs>css>cover.css
HTML
<html lang="en">
{% load staticfiles %}
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Aman Turate">
<title>Aman Turate - Resume</title>
<link rel="stylesheet" href="{% static 'css/cover.css' %}">
This works for me. Update your settings.py to this
.........
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
.........
Make sure the static folder is in the root (i.e where manage.py is)
There are several things to consider here and I am not sure which applies because I'm working on pure assumptions of your situation without knowing:
What is BASE_DIR set to
Have you run manage.py collectstatic
what is your server setup or are you just working off the django development server?
Where are the files you're trying to link to actually placed
Anyway here is some info I hope will be useful. I will break down how the settings files relates to your template file and hopefully that will help you debug your problem.
STATIC_URL = '/static/' -- > the value of this is what will be appended to the static file you are linking in your template. It is the relative url after your domain name. So {% static 'css/styles.css' %} will be rendered as /static/css/styles.css in your html page when it is loaded.
STATIC_ROOT is the absolute path of where your files are located on disk. It tells django where to place all static files collected from your apps when you run manage.py collectstatic see here for details.
STATICFILES_DIRS tells django where to find project static files. By default Django will look for a static directory in each registered app and collect the files in there and place then in your STATIC_ROOT folder. If you want to put static files in a different directory in your project and you want django to know about it then you list those paths in this config variable.
With your code:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static','static_root')
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static','static_dirs')
]
{% static 'css/cover.css' %} is translating to /static/css/cover.css, your telling django the root directory to collect static files is <your BASE_DIR>/static/static_root so here you can see that there might be a miss match in locations. Again I don't know if you ran collectstatic. your STATICFILES_DIR is just going to look for static files in <your BASE_DIR>/static/static_root to then put them in <your BASE_DIR>/static/static_root ... if that makes sense.

django css works on chrome but not firefox

I'm developing django website and am using bootstrap, I can see the styles on chrome browser but not firefox.
firefox inspection shows 404 error for the bootstrap and chrome doesn't. Thankful for any idea.
Code :-
#settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = '%s/coffestatic/' % (BASE_DIR)
STATICFILES_DIRS = ['%s/website-static-default/'% (BASE_DIR),
("bootstrap", '%s/bootstrap' % (BASE_DIR)),]
HTTP file.html
<head>
{% load static %}
<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet">
</head>
Files structure
BASEDIR
|--ProjectFolder
|------Apps
|--BootstrapDir
|------css
Work flow
Define static files
python manage.py collectstatic
Define styles in html
run project
Thanks
I'm not sure if your settings.py files are correct as they are different from how I would set things up. That being said, I'm no expert and you may well be running a different version of django to me. What I did notice though is you don't have a file type on your link file. Maybe Chrome is fine without this but maybe Firefox is having a hissy fit without it. Try changing:
<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet">
to
<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet" type="text/css">
Just a guess....
Using bootstrap starter file in your html file will help; as I have done the same and it helped.
Add these files at the end in body section:
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRS
and add these files at the starting in head section:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
I'm a beginner at django so not sure about it but it worked for me.
In project folder create a directory with any name. Let's name __shared__ and put the css and jss folders inside the directory
and in settings.py
at bottom put this code
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "__shared__"),
]

Python not loading CSS

Ok I am new to Python, I am using Django here.
I cannot get my Python to see the CSS file and use it. I have tried hardcoding the path, server restarts, nothing works. I can get it to read a Bootstrap CDN however, I am not sure what is wrong.
My File structure is like so:
-migrations
-static
--music
---images
---style.css (the file that i'm trying to get)
-templates
--music
---index.html (where my link attribute is)
I am trying to load the static CSS file in index.html like so:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href= "{% static 'music/style.css' %}"/>
Here is the CSS:
body {
background-color: red;
}
Thanks in advance!
Add following code in setting.py file
STATIC_ROOT = os.path.join(BASE_DIR,"deploy_to_server")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
And use in your template
<link rel="stylesheet" type="text/css" href= "{% static 'music/style.css' %}"/>
Create static folder in your project
==> static ==> music ==> style.css
==> manage.py
Hope this helps you

Django CAN find my static files, Pycharm CANNOT resolve them

I do not think this is a duplicate. I have looked at a lot of answers to similar problems.
Please note: This runs perfectly fine and django finds all static files.
UPDATE: It looks like this is a PyCharm bug. If I move static into my app directory, the PyCharm can resolve it. But, I have this static in the main src dir because multiple apps will be using the CSS. Right now it runs and Django has no problem with this. I am thiking this is a PyCharm bug.
PyCharm is set up, and everything else seems to work as far as the Django project goodies that Pycharm offers.
Pycharm cannot resolve, whch means that I cannot use auto complete, and it is annoying. If I hit ctrl-space the only directory that pops up is admin. This means that PyCharm is completely ignoring my static directory.
I have also tried making it a templates directory and a resourse director. No luck there either.
{% load static %} <!-- {% load staticfiles %} -->
...
<link rel="stylesheet" href="{% static ''%}">
<link rel="stylesheet" href="{% static 'css/skeleton.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
...
The code above works fine. PyCharm does not find the urls though.
INSTALLED_APPS = [
'django.contrib.staticfiles',
# admin specific
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_URL = '/static/'
Here is the key to solving the problem: Pycharm needs to know where your settings files that you're currently using is located. See the picture.
Also make sure that you have everything setup in your settings file correctly. See the picture.
In some situation this way can work:
<link href="{{ STATIC_URL }}" rel="stylesheet" type="text/css">
If you have setup everything correctly according to Django doc at:
https://docs.djangoproject.com/en/3.1/howto/static-files/
Then you can try to explicitly set your static folders to help the IDE like:
STATICFILES_DIRS = (
BASE_DIR / "app1/static",
BASE_DIR / "app2/static",
)
this works for me many times. IntelliJ just could not automatically find my app's static.
PyCharm recognises my static files for a configured settings.STATIC_ROOT as below
STATIC_URL = '/assets/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Try loading your staticfiles:
{% load staticfiles %}
And setting your link as:
<link rel="stylesheet" type="text/css" href="{% static "css/default.css" %}">
I used:
STATIC_ROOT = os.path.join(BASE_DIR, "static_deployment")
python manage.py collectstatic command
So I did what needs to be done to go from development to deployment configuration.
pyCharm now works fine and can see static files
Yes it is not the best solution if you change static files a lot but can help developing with pyCharm and Django...
Check in settins.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'your_app/static/')
]
and add
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
Pycharm use this settings.

Categories

Resources