0

As I said in the title I can't import my css file in TINYMCE_DEFAULT_CONFIG variable 'content_css'.

I'm using django-tinymce4-lite package and setting 'content_css' in my settings.py file

I've tried with the boostrap cdn like this:

'content_css': 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'

and it worked but if I do something like this:

"content_css": os.path.join(STATIC_URL, "css/style.css")

I have the following 404 error:

"GET /static/css/style.css HTTP/1.1" 404 1764

my css file is in a static folder located in the root directory of my project like this:

/static/css/style.css

and my Static conf is:

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

urls.py

urlpatterns = [
    path("grappelli/", include("grappelli.urls")),
    path("admin/", admin.site.urls),
    path("admin/filebrowser/", site.urls),
    path("tinymce/", include("tinymce.urls")),
    path("page/", include("pages.urls")),
]
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

It's been a whole day looking for a solution, even the smallest clue is welcome. Thanks

EDIT:

I'm in DEBUG mode running 'python manage.py runserver'

Pipfile

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
django = "*"
black = "*"
psycopg2-binary = "*"
pillow = "*"
django-tinymce4-lite = "*"
django-filebrowser-no-grappelli = "*"

[requires]
python_version = "3.6"
Paul Choppin
  • 116
  • 1
  • 11
  • Note you shouldn't use `os.path.join` to create a URL because URLs always use forward slash, whereas some OSs (Windows) use different path separator (just `STATIC_URL + "css/style.css"` should do the trick), but that doesn't solve this particular issue. Do your other static files get served correctly (e.g. ``)? – dirkgroten Apr 02 '19 at 15:26
  • yes my other static files are served correctly i've tried before exactly what you suggest and it works just fine, my templates receive the css with `{% load static %}`. i also tried `"content_css": f"{STATIC_URL}css/style.css",` and `"content_css": f"{STATIC_ROOT}css/style.css",` and nothing works. – Paul Choppin Apr 02 '19 at 15:31
  • and what do you see as GET request path in your console for those other static files? – dirkgroten Apr 02 '19 at 15:32
  • in other cases i have `"GET /static/pages/css/page_style.css HTTP/1.1" 304 0` – Paul Choppin Apr 02 '19 at 15:36
  • so you have a directory *css* at the same level as the directory *pages*? `"content_css": f"{STATIC_URL}css/style.css"` should be correct. – dirkgroten Apr 02 '19 at 15:37
  • check that your server is restarted if it doesn't restart automatically when you make a change in *settings.py* or when you add a file in your *static* directory. – dirkgroten Apr 02 '19 at 15:40
  • I see a 304 response, are you using whitenoise? – dirkgroten Apr 02 '19 at 15:40
  • i have a `static/css` directory at the root of my project, and a `static/app_name/css` directory in every app of my project – Paul Choppin Apr 02 '19 at 15:41
  • I edited the question whith my pipfile i don't know if any of these packages uses Whitenoise – Paul Choppin Apr 02 '19 at 15:44
  • then you need to add that `STATICFILES_DIRS` because by default django only looks for static files in the static directories of each app, and so does `runserver`. Also you should change `STATIC_ROOT` to not be the same as this *static* directory, but that will only be relevant when you deploy to production and run `collectstatic`. Usually it would be somewhere outside your project (code) directory. – dirkgroten Apr 02 '19 at 15:46
  • yes i tried to setup `STATICFILES_DIRS`but it gave me an error, as you say i have to change the `STATIC_ROOT` but i don't get what it means, do i have to change the name of the static directory at the root of the project ? i did it like this : `STATICFILES_DIRS= [os.path.join(BASE_DIR, "static")]` – Paul Choppin Apr 02 '19 at 15:49

1 Answers1

1

Add STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), ) to your settings.

Also remove STATIC_ROOT for your development settings or change it to something that's outside the project directory, for example one level higher:

STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'static'))

This will make django collect static files to a static directory next to your project directory (but it could be a different location altogether, where your webserver will fetch them directly when you don't run runserver)

Note I wrote a more thorough explanation of the various settings, especially when deploying to production here on SO and here in a blog post

dirkgroten
  • 20,112
  • 2
  • 29
  • 42