How to stop Django from serving files locally? - python

I need to prevent local Django/runserver from serving my static files (I want to test whitenoise locally). The --nostatic flag is supposed to do just that.
python manage.py runserver --nostatic
But I still get 200s (successful) for my static files. So then I set debug = False but still get 200s! I even commented out 'django.contrib.staticfiles' from INSTALLED_APPS. But this did not work either. How could my static files still be served successfully - it's usually not this hard to break things. Perhaps I am misunderstanding what nostatic actually does; I was expecting to get 404s.

Resolved in the comments, but for future reference the answer was to make sure that whitenoise.middleware.WhiteNoiseMiddleware was removed from the MIDDLEWARE list.

Related

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.

.htaccess seemingly ignored on OpenShift

I'm working on a site on OpenShift (Django, Python 2.7, Apache), and I'd like it to always be served over HTTPS. I figured this would be a simple task using .htaccess, and OpenShift's knowledge base seems to show that it should be. However, I've been agonizing over this for days, and I can't figure out how to make it work. Basically, I can't get my .htaccess files to operate on anything but static files. I simply can't get requests for non-static resources to redirect to anything.
Here's my directory structure, inside ~/app-root/repo. It's using the "new" (March 2014 or so?) OpenShift directory structure.
/MyProject
settings.py
urls.py
[etc.]
/[app directories]
/static
/wsgi
wsgi.py
static -> ../static
manage.py
requirements.txt
Ok, so after much frustration, I've been testing with a minimum viable .htaccess to just see if I can get anything to work. It looks like this:
RewriteEngine on
RewriteRule .* http://www.google.com
Everything should go to Google. If I put this in the wsgi directory, all my static files get redirected to google.com, but nothing else does. If I put it in ~/app-root/repo, nothing at all happens. It's like the file doesn't exist.
Similarly, if I use the commands suggested by OpenShift and put the file in wsgi, my static files get redirected to use HTTPS, but nothing else does.
I've also tried WSGI and Django things like adding os.environ['HTTPS'] = "on" to wsgi.py as suggested by sites like this one. That's been ineffective, and I really can't say I'm knowledgable enough about those sorts of settings to know what I'd need to add, but ideally I'd like to do this through .htaccess. The request should be using SSL before it hits WSGI.
What do I need to do to make my .htaccess files work on all requests? And why is OpenShift ignoring .htaccess files not in wsgi?
I fought with this for a short time and quickly decided to use the django-ssl-redirect middleware package.
The whole reason I'm on OpenShift is to avoid fighting with these kinds of configuration issue, so I'm a little disappointed I had to resort to this.
Still, here goes:
Just add this to your MIDDLEWARE_CLASSES
'ssl_redirect.middleware.SSLRedirectMiddleware',
Add this to requirements.txt
django-ssl-redirect==1.0
Add something like this in settings (I put this in my base settings file that I share in multiple environments, hence the "if").
if os.environ.has_key('OPENSHIFT_REPO_DIR'):
SSL_ON = True
SSL_ALWAYS = True
More info here: https://bitbucket.org/nextscreenlabs/django-ssl-redirect

Apache server does not connect to Django

So, I'm running Apache2 on a Linux machine, and I'm trying to serve pages with Django 1.3. I found a guide to do this here.
I have the django.wsgi configured, the settings.py configured, and the database created and successfully in sync with Django. However, when I try to visit the website, I am shown a page served by Apache, instead of Django. I get no errors/warnings at all.
I put print statements in both django.wsgi and settings.py (since they're both just python files), but nothing gets printed.
Does anyone have any idea as to what may be going wrong or any diagnostic steps I might be able to take?
Thanks!
As everyone has said in the comments, you need to add a WSGIServerAlias directive to the Apache configuration before anything will work. Otherwise, Apache can't possibly know to use WSGI to serve your site.

Categories