Django runserver, and s3boto backend - python

Django's "manage.py runserver" has the wonderful ability to magically gather altered CSS, JS, and image files as it's running. This makes the save changes-reload cycle incredibly fast when doing front-end development (css especially). It's wonderful.
However, we've moved to Heroku recently, and installed django-storages with s3boto to handle static files. It works wonderfully too. However, "manage.py runserver" is no longer automatically showing us updated files. We have to run "manage.py collectstatic" to do so -- that works, but it adds an extra step, and slows down development while we wait for the collectstatic step to upload files to S3.
Is there any way to get the old behavior of runserver back, while keeping django-storages?

Related

Why not use "runserver" for production at Django?

Everywhere i see that uWSGI and Gunicorn are recommended for production mode from everyone. However, there is a lot more suffering to operate with it, the python manage.py runserver is more faster, simpler, and the logging is also more visible if something goes wrong. Still, why not recommend the "python manage.py runserver" command for live production?
The runserver management command is optimized for different things from a web-server. Here are some things it does that are great for local development but would add unnecessary overhead in a production environment (source):
The development server automatically reloads Python code for each request, as needed
When you start the server, and each time you change Python code while the server is running, the system check framework will check your entire Django project for some common errors
Serves static files if the staticfiles contrib app is enabled (in a manner the docs describe as "grossly inefficient and probably insecure")
Meanwhile, production web-servers are designed to handle massively parallel workloads and are also under much higher security standards as they are the entry-point for all port 80/443 traffic to the server

Deploying static files for a Wagtail application on Divio

I'm struggling to understand how I can implement my static files live. This is my first project I'm trying to deploy so it's possible I've missed something, and I'm finding it hard to understand which documentation is best to follow here - Wagtail, Divio or Django?
I can view my website with the localhost fine, the static files are read. But when deploying to Divio’s test servers, no longer, just Bootstrap stylings. Am i meant to set debug to False somewhere, and if so where do I set it so?
The dockerfile in the Divio project contains this command, which I sense is related to deploying live:
# <STATIC>
RUN DJANGO_MODE=build python manage.py collectstatic --noinput
# </STATIC>
What are the steps needed to transition from operating on the localhost and viewing my static correctly, to having it display in test/live deployments?
I thought I could link them with the settings.py file but when I try to do this I experience a problem related to the following step:
Step 7/7 : RUN DJANGO MODE=build python manage.py collectstatic —noinput
It seems to hang almost indefinitely, failing after a long time - the following are the last few lines of my logs.
Copying '/virtualenv/lib/python3.5/site-packages/wagtail/admin/static/wagtailadmin/fonts/opensans-regular.woff'
Copying '/virtualenv/lib/python3.5/site-packages/wagtail/admin/static/wagtailadmin/fonts/wagtail.svg'
Copying '/virtualenv/lib/python3.5/site-packages/wagtail/admin/static/wagtailadmin/fonts/robotoslab-regular.woff'
Copying '/virtualenv/lib/python3.5/site-packages/wagtail/admin/static/wagtailadmin/fonts/opensans-semibold.woff'
Thanks all in advance for your time and help!
In a Divio Cloud project, the settings for things like static files handling and DEBUG are managed automatically according to the server environment (Live, Test or Local).
See the table in How to run a local project in live configuration. You can override these manually if you need to, but there is no need whatsoever in normal use.
If you have added settings related to static file handling to your settings.py, try commenting them out - almost certainly, it will just work.

Restarting Python to show changes

I am new to the python script. How do you restart a python script with Django through SSH?
I believe that you project is django based.
Django framework has a project directory, where static files are initially placed. And when you run your project for development purposes, django takes all static from the project static directory.
But for the production django deployment usually get runned command manage.py collectstatic to copy all static into another place. And sometimes there are another command - compress. To compress that static.
This is done to make webserver (apache or ngingx) respond static files without asking django process requests like "five me that static file" and gives ability to cache static files. And it speed-up all work.
So, if you serveer is setted up to take static files from static dir (looks like DOMAIN/public/static/main/ is the static dir) it will have no idea about changes in the project dir (looks like DOMAIN/project_book/main/static/main/ is a project dir).
But I agree with #Sause, looks like you have to be very carefull and have exact understanding of what you're doing with killing any process on the production server.
I think it could be useful for you to read Django documentation about static files too. https://docs.djangoproject.com/en/1.9/howto/static-files/
running pkill python in the ssh works.

Django - Apply changes in HTML & Static files without restarting dev server

I am working in a dev environment using the built in Django web server. One of the inconvenience that I have is everytime I make changes in HTML or Static files it does not apply when I reload the browser until I kill the dev server and run again.
python manage.py runserver localhost:8000
Is there a way so Django will reflect the changes instantly? Thanks in advance
Django reload the server only on changes on .py files. I read different ways to trigger reloading (such as installing a third party app, tweaking caching depending on whether DEBUG = True, etc etc).
The easiest and dumbest way is to make an insignificant slight change in the view you're working on (say, adding a #, removing it) after you edited and saved your template. It is dumb but it works.

Django Debug Toolbar style is broken

I have installed django debug-toolbar, it can fetch the css and js but it still shows debug toolbar like below and also shows this at the end of the page instead of nearby, there is not js error in console. Any ideas?
I was having this exact same issue. You don't need to switch to serving files locally, using S3 to serve your static files works just fine.
In my case (and likely in yours), you just need to run:
$ python manage.py collectstatic
That will update the static files—adding the files that the Debug Toolbar needs to render its styling correctly.

Categories