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/
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):
...
In a django project, I have an app : app_signup.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_signup',
app_signup/urls.py
from django.urls import path, include
from . import views
urlpatterns = [
path('',views.index, name='index'),
]
Is it possible to add an alias to the app in order to see the alias (http://localhost:8000/alias/) instead of the name of the app(http://localhost:8000/app_signup/) in the url on the browser.
The name of the app in INSTALLED_APPS doesn't really matter. Your project's main urls.py file is the one that specifies the path. There when constructing the urlpatterns list, you can do path('alias/', include('app_signup.urls')) instead of path('app_signup/', include('app_signup.urls')).
As far as I understand from the example you added, you are editing the urls.py file within the app you created. Not your app, but directly in the project file (in the same directory as settings.py), urls.py probably as path('app_signup/', include('app_signup.urls')) as mentioned in the answer above. This is the urls.py you need to edit.
I'm new to Django and I'm following this tutorial: https://www.django-rest-framework.org/tutorial/1-serialization/
When I run the server with python manage.py runserver, it seems to work fine showing me no errors.
However when I visit http://127.0.0.1:8000/snippets/ it gives me the following error:
Using the URLconf defined in tutorial.urls, Django tried these URL patterns, in this order:
admin/
The current path, snippets/, didn’t match any of these.
Here's my urls.py located in the tutorial folder:
from django.urls import path, include
urlpatterns = [
path('', include('snippets.urls')),
]
I don't understand what's going on, how come it only checks admin/ if I'm wiring the root urlconf to snippets.urls?
On a related note, when I modify urls.py and add gibberish the server won't give me an error, however if I were to modify models.py then it'll start complaining, that's why I'm getting the feeling it doesn't even check my urls.py and maybe instead some default one...
Any tips are very appreciated!
EDIT:
Here's my urls.py located in the snippets folder:
from django.urls import path
from snippets import views
urlpatterns = [
path('snippets/', views.snippet_list),
path('snippets/<int:pk>/', views.snippet_detail),
]
In settings.py I've only modified the INSTALLED_APPS part with the two last apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'snippets.apps.SnippetsConfig',
]
Are you sure you don't have another instance of Django running?
That's the only thing I can think of since admin isn't even in your current urls
My settings.py file has the static URL set:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
The staticfiles app is installed:
INSTALLED_APPS = [
'Journal',
'adminsortable2',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
I've created a folder inside my app called static/journal:
https://i.stack.imgur.com/T1eGw.png
My template loads static and uses the URL as per documentation:
{% load static %}
<link rel='icon' href='{% static "journal/favicon.ico" %}' type='image/x-icon'>
And yet when I browse to http://localhost/static/journal/favicon.ico I get an error 404!!!
The rest of my app is accessible at http://localhost (runserver is on port 80 and localhost resolves to 127.0.0.1 via hosts file_
You need to add static file urls to url urls.py.
In your urls.py:
At the top of the file put these lines:
from django.views.decorators.cache import cache_control
from django.contrib.staticfiles.views import serve
from django.conf import settings
from django.conf.urls.static import static
At the bottom of the file put these lines:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
view=cache_control(no_cache=True, must_revalidate=True)(serve))
from above your question it seems that you are not using the Port number in the url . Can you try http://localhost:80/static/journal/favicon.ico .
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"?