I have javascript files in my static folder. Django finds and loads them perfectly fine, so I don't think there is anything wrong with my configuration of the static options. However, sometimes when I make a change to a .js file and save it, the Django template that uses it does NOT reflect those changes -- inspecting the javascript with the browser reveals the javascript BEFORE the last save. Restarting the server does nothing, though restarting my computer has sometimes solved the issue. I do not have any code that explicitly deals with caching. Has anyone ever experienced anything like this?
I believe your browser is caching your js
you could power refresh your browser, or clear browser cache?
on chrome control+f5 or shift + f5
i believe on firefox it is control + shift + r
Since you are editing JavaScript files and watching for the changes in the browser I assume you are actively developing your Django app and probably using Django's development runserver. There is a better solution than clearing the browser cache and refreshing. If you run a watcher utility that supports the livereload protocol then your browser will automatically refresh whenever you change any static file.
The django-livereload-server python package provides a spiffy solution. Install it:
$ pip install django-livereload-server
Add 'livereload.middleware.LiveReloadScript' to MIDDLEWARE_CLASSES in settings.py.
Then run
$ ./manage.py livereload
before starting the runserver.
More documentation can be found at the django-livereload-server github site
For me, opening Incognito Mode in Chrome let the browser show the recent changes in my .js static files.
To anyone who is using Firefox:
If you don't want to clean your browser cache or it doesn't solve your issue, you can try doing a hard refresh (hold Shift and press the refresh button).
Related
I'm working on my first real django project, and was fiddling around with the css, but the changes I made did not have any effect on the site. I tried changing things on all 3 of the css files but nothing changed. I then deleted the files (from the project I still have them), and still nothing changed. My site is getting styled, I checked all of my html to make sure there was no inline style in there. All the html files are working, when I change them the site changes. Is this some weird caching thing? I've tried rerunning the server several times and still my site is getting style, seemingly from nowhere. Anyone have any ideas?
Try opening your site from now on in incognito mode. I think it is some weird caching thing so maybe clear your cache too. When developing I just use igcognito and static updates just fine.
Django copies static files (like CSS) in a separate directory when you're running with DEBUG=False. If you update any of the static files in your project, you'll have to run the collectstatic command to update them.
From the root of your project, try:
./manage.py collectstatic
I'm stepping into the Python world. First I learned a little Python. Then I learned the basics of Django, and on top of that I'm learning Wagtail (framework for template managing for Django)
To learn Django a went through a tutorial to build a site locally and test it in 127.0.0.1:8000.
At some point of the tutorial I configured the settings (because the tutorial said so) to redirect to 127.0.0.1:8000/catalog when browsing to 127.0.0.1:8000 alone.
Then I started the Wagtail tutorial, as a completely different project in another folder. Not sharing any code with the tutorial Django project.
I run the server and the console says it is now running in port 127.0.0.1:8000 and when I browse it, it redirects me to /catalog and of course shows a Page not found error since this project doesn't have one app catalog.
I workaround this by opening Chrome in Incognito Mode. But still I would like to know why this is happening and how to solve it to add to my knowledge of how Python works.
Some notes:
I'm on Windows
I killed all processes related to Python and actually this is still happening after turning my PC off and on
I know I could use a different port, please do not give me that answer. My goal is to learn.
Try clearing your browser cache.
The problem is most likely that your browser has cached a 301 Moved Permanently for 127.0.0.1:8000, thus never hit your development server when you enter the URL, but simply do the redirect based on the cache.
You will see the same result no matter what you're running behind port 8000, and it's as such not related to Django.
A little background:
I've been working on this project for about six months now and it's been running on Flask the whole time. Everything has been fine, multiple versions of the backend have been deployed live to support an app that's been in production for months now.
The development cycle involves writing everything locally and using Flask-Script's runserver command to test everything locally on localhost:8080 before deploying to a dev server and then finally to the live server.
The Problem: The other day my local flask instance, which runs on localhost:8080 apparently stopped respecting my local files.
I tried adding a new view (with a new template) and I got a 404 error when trying to view it in my browser.
I then tried making some test changes to one of the existing pages by adding a few extra words to the title. I restarted flask and none of those changes appeared.
I then went as far as deleting the entire views.py file. After restarting flask again, much to my dismay, I could still view the pages that were there originally (i.e. before this behavior started).
Finally, I made some changes to the manage.py file, which is where I put all of the Flask-Script commands, and they weren't recognized either. It's as if flask started reading from a cached version of the filesystem that won't update (which very well might be the case but I have no idea why it started doing this or how to fix the issue).
FYI: Browser caching shouldn't be an issue b/c I have the dev tools open with caching disabled. Plus the fact that changes to manage.py aren't being noticed shouldn't have anything to do with the browser.
You've most likely used flask in the DEBUG mode in which it auto reloads templates the app whenever a file changes.
Try using
export FLASK_DEBUG=True
before running
flask run
For more information see http://flask.pocoo.org/docs/1.0/config/#DEBUG
I was having a similar issue and deleting the .pyc files solved it for me.
I have several django projects and they work well on my desktop. But when I run them on my laptop, they run ok for sometime. Then on a random occasion, opening a page won't work. The browser keeps trying to load the page (title tab keeps spinning, URL changes to the page its trying to open, and the page turns blank), while the development server (django on windows shell) says it has successfully served the page (200 status).
This behavior is consistent among Firefox, IE and Chrome. I tried changing ports, using machine IP instead of localhost, loading static files on external server, but nothing works. I tried opening the site (using laptop computer name) from desktop browsers and behaves the same. Another interesting thing is, even if I shutdown and restart the django server, I wont be able to open the page that have failed previously unless I close the loading page.
My laptop is running a basic Windows 8, while desktop is Windows 8 Pro. I think the windows version has something to do with it.
Does anyone know how to solve this? I hope I made myself clear. Thanks.
It is hard to tell whether the issue is related to Windows specifically, rather than compatibility issues with images/CSS/Javascript/plugins such as Flash. Are you running the latest versions of those browsers (or at least the same versions as on your desktop)? Do you have different security software/firewalls? Do other sites load inconsistently? Seems unlikely to be a Django issue (although you can try loading sites like djangoproject.com).
Thanks people for your comments and answer. I uninstalled from the laptop each application that is not present in the desktop and found which one is causing the problem. This app called NetWorx has a network filtering that I enabled and for some reason its blocking the django response. I disabled network filtering which is good enough for my need.
How to automatically monitor .py, .js and other source code files to restart a Django (or any other for that matter) application and refresh the browser when the source changes? This is possible in Rails using guard, in JS apps using grunt-contrib-watch and the accompanying livereload browser plugin. How can I do it for Python web apps such as Django?
I start my Django server with
foreman start
this is my Procfile:
web: newrelic-admin run-program gunicorn app.wsgi
as suggested by the Heroku/Newrelic docs or the usual
python manage.py runserver
The runserver method does restart the server on .py source changes, but not the browser and doesn't watch other files - I could run guard alongside it, but then I have two processes I have to take care of, whereas grunt or rake offer unified interfaces. I'm wondering what is the recommended way of doing this among Python developers?
I could not find any detailed, comprehensive documentation on this - only incomplete discussions here and there.
You don't need a browser extension to accomplish auto refreshes. Take a look at https://github.com/tjwalch/django-livereload-server.
I posted a more extensive answer about this at https://stackoverflow.com/a/36961244/2950621
It works by using a manage.py command (server) to monitor your .js and other static files. The server sends a signal to the browser via websockets. There is some client-side code injected on every page. The injected code responds to the signal and refresh the browser.
Install this django app:
pip install django-livesync
On your django settings file add something like:
INSTALLED_APPS = (
'...',
'livesync',
'django.contrib.staticfiles',
'...',
)
MIDDLEWARE_CLASSES = (
'livesync.core.middleware.DjangoLiveSyncMiddleware',
)
Beware to register 'livesync' before 'django.contrib.staticfiles' if you are using it.
Now, just start your development server:
python manage.py runserver
Check this out for more details: https://github.com/fabiogibson/django-livesync
Using python manage.py runserver is what most use. You'll have to use another tool like: http://livejs.com/ to refresh the browser itself since Django really isn't aware of it.
Frustrated with all the explicit refreshes, I created a browser extension, for both Firefox and Chrome, to automate this. The extension works with a Django app that you add to your app list in INSTALLED_APPS. You can find out more at the github repo.
Though the repo has entire source code, the extensions are also available in the respective web store. Just search for 'Django Auto Refresh'. With these, you just need to copy the app into our project's folder and include it via INSTALLED_APPS. I wanted to add a pip setup script, but haven't found the time to do it.
HTH. Apologies if this sounds like self promotion.
I tried several answers here. But the browser did not seem to show the recent changes of the code. It worked for me when I opened Chrome in Incognito Mode.
If you are using Visual studio code, then you can just use python manage.py runserver and use VS code's auto save feature.
With this feature, whenever you'll make changes in your code, it will save everything and trigger a server reload.