How to autorun collectstatic on local django instance? - python

While developing locally on a side Django project, my workflow is currently this:
Make changes.
Go to Terminal session with virtual environment. Stop foreman with ctrl+C.
Type python manage.py collectstatic to move all my static css/js/img files.
Restart foreman with foreman start.
In an attempt to be more efficient and do a better job of learning, I'm wondering how I can optimize the workflow is it's more like this:
Make changes.
Run a single command that moves static files and restarts foreman.
Would someone be able to point me in the right direction. Thanks.

You could create a bash script that does this bunch of commads for you.
While I have no experience with foreman, you could create a script with content something like:
#!/bin/bash
sudo killall foreman
python manage.py collectstatic
foreman start
Then add execution rights to it:
chmod +x script.sh
And execute everything in one command:
./script.sh

I assume you absolutely can't get around using foreman for local development, cause otherwise you would not even need to do a collectstatic or manual restart.
Maybe writting a custom management command based on runserver is the way to go for you, as it would already have the check-for-change-and-restart logic in it.
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/
https://github.com/django/django/blob/master/django/core/management/commands/runserver.py?source=cc

Related

Is there a way I can avoid stopping and starting 'python manage.py runserver' on Django everytime I make a change?

Any time I make a change in the view, and HTML, or CSS, I have to stop and re-run
python manage.py runserver
for my changes to be dispayed. This is very annoying because it wastes a lot of my time trying to find the terminal and run it again. Is there a workaround for this?
python manage.py runserver should normally perform hot reload on your Django application, except you've updated the config in the settings.py file. Check if DEBUG = True in settings.py
My advice is to use Vscode for Django developing because it gives you autosave feature so you don't have to stop and rerun the server the only thing you have to do is reload the web page. I hope it might be helpful

Django 'collectstatic': is it a bad practice to automate it?

I have a Django project as a webserver (Apache2) on a Linux machine.
The server needs to periodically analyze some files outside the Django project folder, so I'm running python3 manage.py collectstatic to gather all files into my static directory.
I couldn't find anything related to this, so I would like to know if is this a Bad practice or if already exist a better way to do this...
Should I add a cronjob or systemd service to execute python3 manage.py collectstatic --noinput and automate this process? Is there a better alternative?

following python flow of execution when project is run

I want to start on an open source project.
is there a way i could follow the flow of execution of code in python when its run.
Using sys.set_trace() i am able to follow the trace of a code blocks in a script. I want to bump this up to bigger projects lets say for example a django app.
a simple example given the command python3 manage.py runserver when run with "trace" - an imaginery tool
trace python3 manage.py runserver would listen for function and method calls from in project after the command and maybe print them out somewhere.
What would be the best way to go about implementing this.
my idea is creating a virtual box like docker where i would listen for any python calls.
Does anyone have a better way or input to help me on this
i would highly appreciate it

How to clear code cache in python/django?

Sorry, I'm newbie on python.
I change code in .py file but no changes happens.
Guess, script code is cahced in memory.
What should I do to make my changes working?
PS: tried to delete complied .pyc file, no luck 8-(
guess, IO need to try to restart uwsgi/nginx?
Older versions of django
find . -name "*.pyc" -exec rm -rf {} \;
New version of django has this:
python manage.py clean_pyc
If you simply want to restart the webserver it depends on the configuration you are using:
via gunicorn:
use --reload option
via django inbuilt runserver:
since version 1.7 it automatically does that.
Just touch the WSGI file: touch wsgi.py and your project should refresh.
Please have a look at this similar request: How to reload new update in Django project with Apache, mod_wsgi?

Django application deployment help

I'm using Capistrano to deploy a Django application (it uses Nginx as the web server), using instructions I found at http://akashxav.com/2009/07/11/getting-django-running-on-nginx-and-fastcgi-on-prgmr/ (I had to look at a cached version earlier today) and was wondering about the last command in there, which is
python manage.py runfcgi host=127.0.0.1 port=8081 --settings=settings
I understand at a high level that this is telling the application that we want to run a few instances of the FastCGI binary to serve up this application.
What I was wondering is how is the best way to handle "resetting" this, for lack of a better word. For those who don't know, Capistrano deploys things by creating "releases" directories and then providing a symlink to the latest release.
Since I can do post-deployment tasks (I've done this with CakePHP applications to do things like properly set directory permissions for a caching directory in the application) I was wondering how to turn off the existing processes created by the command above and start up new ones.
I hope I am making sense.
There is a section in the django docs about this
Basically use the pidfile option to manage.py and then write a small shell script to use that pid to kill the existing cgi process if it exists before starting the new one.
Something like this
#!/bin/bash
if [ -f "pidfile" ]; then
kill `cat -- pidfile`
rm -f -- pidfile
fi
exec python manage.py runfcgi host=127.0.0.1 port=8081 pidfile=pidfile --settings=settings
NB FastCGI support is deprecated and will be removed in Django 1.9

Categories