Autocomplete is not working for static files for me with Django 2.0.
I'm using static files in my project with the current structure
project
-app_1
-templates
--base.html
-static
--bootstrap
---bootstrap.min.cs
---bootstrap.min.js
Here's the HTML code where autocomplete doesn't work. Am I doing something wrong?
The files are linked properly and I'm getting the bootstrap design, the problem is that the autocomplete isn't working.
Here's my static settings
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(os.path.join(BASE_DIR, 'static')),
)
I've been able to resolve this on two different computers and projects in two different ways.
The first project uses settings module instead of a single file like this
settings
- init.py
- shared.py
- prod.py
- dev.py
Autocomplete for static started working after I set the proper path to settings like this:
In the second instance I had a settings.py file and autocomplete started working when I replaced my AppConfig with app name in settings INSTALLED_APPS like this, though I'm inclined to think this is just some kind of bug:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'depapp', # Used to be depapp.apps.DepappConfig
]
Did you mark the directory as "Resource"?
Related
I am using Whitenoise to serve static files (images, css, js) for a Django site. Now my problem is that the static files seem to be "cached to much"(?) when working locally. Description of actions:
Initially my static files are served correctly
I edit a file in static/
I run ./manage.py collectstatic (which correctly identifies one updated file).
When going to http://127.0.0.1:8000/ my browser consistently shows the old stale version of the file. I have even tried completely removing the generated staticfiles/ folder - and the browser still seems to be able to dig out an old version of the file?
This is when running locally in debug mode. Do not have a consistent understanding of how it is in production, but I think it works better (as it should?) there.
My configuration:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
....
INSTALLED_APPS = [
# My apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
...
STATIC_URL = 'static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [
("js" , BASE_DIR / "static/js"),
("css" , BASE_DIR / "static/css"),
("img" , BASE_DIR / "static/img")
]
I guess the problem is that I do not really understand the Whitenoise model - slightly frustrating as this should be quite simple??
Update: I have tested this in Firefox with Ctrl-Shift R to reload the page, and got the old version. But when actually deleting the browser cache explicitly things work. feels a bit excessive that I have to manually wipe the browser history - but I can live with that.
If a hard reload works, then the browser is most likely caching the parent request (rendered html result).
So if you have an endpoint, /myendpoint/, that returns a rendered template, where the template contains the static file reference handled by whitenoise , css/mycss.{hash}.css, the parent request is being cached and the browser doesn't see the updated static file reference.
Django has some tools that help dealing with browser-side caching.
https://docs.djangoproject.com/en/3.2/topics/cache/#controlling-cache-using-other-headers
If you want to make sure the client always get's the latest page you can use the never_cache decorator.
from django.views.decorators.cache import never_cache
#never_cache
def myview(request):
...
I have no idea, this always worked for me, but without motivation, it's not working now.
what i did:
I created my project
I created my app
I added my config to INSTALLED_APPS
I get this error: django.core.exceptions.ImproperlyConfigured: 'champ.apps' does not contain a class 'ChampConfigcorsheaders'. Choices are: 'ChampConfig'.
My project looks like this:
Championship_3bi
champ
all the files of the app
Championship_3bi
all the files of the project
This is my settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'champ.apps.ChampConfig' #this is the line that create the "error"
'corsheaders',
]
I've also tried to do champ.apps.ChampConfigcorsheaders but it didnt work.
This is my champ/apps.py:
from django.apps import AppConfig
class ChampConfig(AppConfig):
name = 'champ'
i searched for everything but looks like i was the only one who get this error.
The reason why it does not work is for the weird name of my project?
Why it is not working for only this project?
im done
You are missing a ,:
'champ.apps.ChampConfig',
'corsheaders',
I'm working with Django and I'm using a AdminLTE framework for the estilização, but, when I turn the DEBUG to False, the page shows with pure html.
DEBUG = True:
DEBUG = False:
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'saga.core',
'saga.subjects',
'saga.accounts',
'saga.tasks',
'saga.study_time',
'django_adminlte',
'django_adminlte_theme',
]
django_adminlte and django_adminlte_theme are the apps for the style framework.
When DEBUG = True Django will serve static and media files. When DEBUG = False it will not. Therefore all of the js and css files will return a 404 error unless served. For a better of understanding of what is going on i would recommend reading https://docs.djangoproject.com/en/1.11/howto/static-files/ These static files may also be present in installed apps and not just within the project itself.
For local testing you can add the following to your urls.py urlpatterns:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
static can be imported from from django.conf.urls.static import static
Further information can be found at https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development
Further information regarding how to deploy static files in production can be found at https://docs.djangoproject.com/en/1.11/howto/static-files/deployment/
This is my installed apps section in settings.py.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'homepage.apps.HomepageConfig',
.
.
.
]
My app name is "homepage" and I really can't remember why I changed it to 'homepage.apps.HomepageConfig', but whatever it is, it worked on my machine.
Now, i uploaded my files to server, installed required apps, did the migrations, but i noticed django does not create my "homepage" app table and does not migrate anything from my app. And my website returns the error: table homepage_post does not exist.
What is wrong?
Check if in homepage app directory You have file named __init__.py and apps.py. The content of apps.py should be:
from django.apps import AppConfig
class HomepageConfig(AppConfig):
name = 'homepage'
I want to use static files to load my css with runserver command. The problem is that i tried all the solution i found here on stackoverflow and also in django docs, but it doesnt works at all... I dont know what can i do...
If i set
STATIC_URL = '/static/'
STATIC_ROOT = 'C:\Users\Max\Works\www\mysite\mysite\static'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
)
I thought it was enough... Am i missing something?
Can you tell me what is the best setting for have static files in develompent envirnoment?
Thanks in advice...
EDIT(1)
I already putted in my template {{ STATIC_URL }}css/dark-grey.css" and ofc the css is in C:\Users\Max\Works\www\mysite\mysite\static\css\dark-grey.css, i really can't get what is wrong...
Use / slashes and NOT \ slashes in the path, even for windows paths.
In your settings.py
DEBUG=True
As per the docs:
This view is automatically enabled by runserver (with a DEBUG setting
set to True).
Using the URL pattern is a way to force it, which I personally don't even have to do in my project as long as DEBUG=True. You would always have DEBUG on when you are developing, and when you switch to production you aren't even using the development server anyways, so you would be pointing your production server to the static location.
This is a snippet of my static settings from my settings.py. I do not manually have to add that static view URL
import os
DEBUG = True
PROJECT_ROOT = os.path.dirname( __file__ )
PROJECT_NAME = os.path.basename(PROJECT_ROOT)
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_ROOT, 'web/'),
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
...
...
)
You need to add url patterns:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf here ...
urlpatterns += staticfiles_urlpatterns()
See the documentation here
You also need to run the following command to get the static files moved into the right place (and for Django to know they're there):
python manage.py collectstatic
Full documentation on static files in Django 1.3 is here:
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/