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.
Related
I'm having problems with my Django > css its not loading on the page for some reason am I missing something ?
I have attached snippets of my current code
Settings.py
urls.py
.html
put in you'r setting.py
INSTALLED_APPS = [
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = [
STATIC_DIR
]
then create folder static/file.css(nameyourfile.css)
and then put in your ...html :
<link rel="stylesheet" href= "{% static "file.css" %}">
{% load sataticfiles %}
if you use version 3. put :
{% load static %}
You need to do a few more things to configure your static files.
First, you need to make sure django.contrib.staticfiles is included in your INSTALLED_APPS in your django settings.
Also, you need to add this line in your html document:
{% load static %}
I would follow this guide which explains static files in the django docs.
The current project I've started working on, uses django 2.2 and most of the links are hardcoded.
All static content is in folder named media and used in base.html as follows
in base.html
{% load staticfiles %} ---- using this as first line of base.html
.......
.......
<head>
<link href="/media/css/backoffice/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
in settings.py
STATIC_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'media'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'media/')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
I've also added following to the main urls.py file:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Yet I'm not able to render the static file listed in the head section of base.html
I've also experimented with commenting out static root and staticfiles_dir but it does not work
You need to put all files in project root's static folder and run this command:
python manage.py collectstatic
For loading static files in the template you need to write this:
{% load static %}
And in all the static files like css, js and images you can do like this:
{% static "<absolute path of file>" %}
And you can refer from documentation.
I hope this will help you!
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.
I know that this problem has already been asked several times here. I have searched and read a number of answers but no help. I guess, I am missing something very basic.
In my settings.py, I have:
STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
# STATICFILES_DIRS = [join(APPS_DIR, 'static')]
MEDIA_ROOT = join(APPS_DIR, 'media')
MEDIA_URL = "/media/"
In my config/urls.py, I have:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have a file located at /static/core/js/jquery_countdown/jquery.countdown.min.js which I am trying to load in template as below:
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="{% static 'core/js/jquery_countdown/jquery.countdown.min.js' %}"> </script>
The top of the same template looks like
{% extends "contest/base.html" %}
{% load static %}
This results in following server error:
[23/Mar/2018 10:12:08] "GET /static/core/js/jquery_countdown/jquery.countdown.min.js HTTP/1.1" 404 1858
What am I missing?
Create folder static_files in your app directory. And place all your static files inside it. Then use the following settings
STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
STATICFILES_DIRS = [join(APPS_DIR, 'static_files')]
If it does not solve your issue, then run the command python manage.py collectstatic. It will copy all the static files (Not only from your app but also from django admin, third party apps etc) to the STATIC_ROOT folder.
Details
For local serving, django server will look in to the STATICFILES_DIRS. So you dont need to run the python manage.py collectstatic command. All the external apps may have STATICFILES_DIRS where they place their static files. For production server you need to collect all these static files scattered around all your apps in to a single place. Thats what python manage.py collectstatic command will do. It will copy all the static files in to STATIC_ROOT directory
You are searching for static files in app directories only but have your file in global static files. You should use the commented STATICFILES_DIRS setting to specify all places to search for static files.
I found that this worked for me. (Development)
(I chose to name it "zStatic" to keep it at the bottom of the root (project folder), so that it isnt amidst all my apps. Easier to locate.)
Settings.py
STATIC_URL = 'zStatic/'
STATICFILES_DIRS = (
BASE_DIR/'zStatic',
)
INSTALLED_APPS =
[
...
'django.contrib.staticfiles',
...
]
base.html (base template)
<head>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/mycss.css' %}">
</head>
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__"),
]