How to automatically reload Django when files change? - python

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.

Related

Django: Push app from local server to production server via FTP

This is a bit embarassing, but I'm a Django noob and I couldn't find a simple solution to this:
I have written a Django app in a local VM that I now want to deploy to a "production" server. App works like a charm locally.
Now my IT colleague has set up the server with Django and that also works fine. I can open it via the Web and I get the usual "Congratulations on your first Django-powered page". I can also log into the admin interface. The project has been created.
This is a very low-key mini project and I'm not too familiar with git, so we've decided to just push files via FTP. (And I want to stick with that if at all possible.) So I uploaded the app folder into the project folder and also adjusted the project's settings.py and urls.py.
However, nothing seems to be happening on the server's end. The welcome page is the same, the app does not show up in the admin interface and the URLs won't be resolved as hoped.
Any suggestions what I should have done / done differently?
You need to restart apache or whatever is running your django project. Your changes to py files are cached when you first load your server config (settings).
Any suggestions what I should have done / done differently?
You should be using git/jenkins/deployment techniques, I know you said you've decided not to use it but you're going to be missing out on important things like being able to keep track of changes and unit testing

How to Access URL of Localhost Django Project - Page Not Found

I transferred my Django project from a Ubuntu server that is currently serving the website. I'm trying to run this project on a localhost MacOSX environment for testing purposes. The command I'm familiar with for Django projects is:
python manage.py runserver
After which when I input 127.0.0.1:8000 into a browser, the project displays.
But my Ubuntu server is not currently running a script in that form, rather in the form of
python manage.py celeryd
My knowledge of how celery interfaces with Django is somewhat limited so this question may be very basic, but I can't find the URL to display the project.
On my Mac server, when I run:
python manage.py celeryd
the script does not error out on me--instead, it appears to be working by displaying this:
However, I don't know how to access the project. When I put 127.0.0.1 in the browser (and many other possible variations using different ports), I get Page Not Found.
Any help is greatly appreciated.
First you need to define 'core' template tag in one of your apps templatetags library say custom_tags.py. Make sure that this app is added to INSTALLED_APPS in settings. Then in your html template add
{% load custom_tags %}
It will fix the error you are getting. For more details please read docs.
You seem confused about what celery is. It is not a webserver, and it won't serve pages for you. It is an offline task manager, useful for performing complex or long running jobs that aren't appropriate to do in the context of a web request.
You'll need an actual web server alongside celery to serve your site itself.

Download and setup a Angular/Django site from server to localhost?

Hi I am new to Django and have been googling for a whole day without any success.
Basically there is a live/working website built with AngularJS, Django and PostgreSQL (Ubuntu 14.04) and I am trying to download all the files and clone the site into my localhost (Ubuntu 14.04).
After I downloaded the folder and finished install the required packages on my localhost, I run:
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
The server runs without reporting any errors.
However, some of the links are directed to a 404 page, such as this:
http://127.0.0.1:8000/city/chicago
Whereas in the live site, it would direct to the correct working page.
Can someone tell me what may have gone wrong in the process?
Thanks.
Try editing the settings.py file set the DEBUG value to True. Instead of a 404 you should get a full debug of the actual request and see what exactly is missing (I suspect missing data as #NightShadeQueen pointed).
Also you might have a look at django debug toolbar which prints the executed sql queries as well and run them against your local database to see what exactly they return.

Herkou site looks different at launch then django local server site

My issue is that when I view my site using python manage.py runserver or foreman start, I can see my site perfectly.
However, when I git push heroku master on the surface everything appears fine as no errors are given. But when I view my site with the Heroku given site link, I do not see my updated site as I see when I view my site using python manage.py runserver or foreman start.
I am building my site using 'pinax-theme-bootstrap` and my virtualenv is on my desktop directory.
Does anyone have a solution as to why this may be the case?
Here is a list of suggestions on how I would approach this issue with Heroku.
You should try heroku restart. This restarts your application and can help pick up new changes.
I would clear my browser cache as often I do not see changes on my web page if the browser has cached them.
I would check that the git repository on Heroku matches my local one in that it has all the newest changes made on my local server.

Django Not Reflecting Updates to Javascript Files?

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).

Categories

Resources