Switching to Amazon CDN from dj-static with django on Heroku - python

I have been developing a django site on Heroku and using dj-static in my wsgi.py. I am now about to move my site static files onto Amazon. Do I need to now remove the references to dj-static from my wsgi.py file? I'm concerned about the following lines of code. What would be the correct thing to do? Do they need to go? If so, what do I put in their place?:
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
Thanks,
Euan

If you are serving files using django-storages you don't need to use dj-static anymore. dj-static is only used when you want to serve the static files using a WSGI server like gunicorn. In your case you are using Amazon's servers.
To answer your question, you can revert to the default wsgi.py file which looks something like this:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
You can also remove dj-static from your virtualenv and from requirements.txt

Related

Issue deploying Django webapp in OVH Hosting

I'm trying to deploy a Django App in OVH Hosting and, after some hard exploration and try-error, I keep getting an issue.
File "/usr/share/passenger/helper-scripts/wsgi-loader.py", line 381, in <module>
handler = RequestHandler(server_socket, sys.stdin, app_module.application)
AttributeError: module 'passenger_wsgi' has no attribute 'application'
OVH/Passenger proccess feedback
Apart from all the files created at startapp by Django and all the code I wrote I added the following file 'passenger_wsgi.py' on the root of the app directory. I used two differents versions:
First:
import MyApp.wsgi
application = MyApp.wsgi.application
Second:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
In the runtime configuration of OVH hosting the application launch script set is 'manage.py'. SECRET_KEY and DJANGO_SETTINGS_MODULE are declared in the environment variables.

Missing variable `handler` or `app` in file "project/wsgi.py"

I have been trying to deploy my Django app to Vercel and I am getting this error. Any idea?
Missing variable `handler` or `app` in file "qr_project/wsgi.py".
So I found out that vercel looks for app not application (which is comes default with django-admin startproject mysite). So I changed my wsgi.py file like this
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')
application = get_wsgi_application()
app = application

After splitting settings.py file, mod_wsgi failed to exec Python script file. And application = get_wsgi_application() errors occured over and over

Hi everybody I tried to deploy my django project with apache, mod_wsgi in windows.
I splited my settings.py like this:
source root folder
project folder
apps
config
settings
init.py
base.py
local.py
prod.py
init.py
asgi.py
urls.py
wsgi.py
myenv
After I splited settings.py,
Mod_wsgi failed to exec python scripts file :
'C:/user/users/desktop/source_root_folder/project_folder/wsgi.py'.
Mod_wsgi also show the exception that Exception occurred processing WSGI script :
'C:/user/users/desktop/source_root_folder/project_folder/wsgi.py'
In Apache24 error log, 'C:/user/users/desktop/source_root_folder/project_folder/wsgi.py'
application = get_wsgi_application()
error occured.
# project_folder/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
import sys
sys.path.append('C:/Apache24/htdocs/ C:/user/users/desktop/source_root_folder')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project_folder.config.settings'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_folder.config.settings')
application = get_wsgi_application()
how can i deal with those errors?
please give me some helps.
you need to specify the *.py settings file:
os.environ['DJANGO_SETTINGS_MODULE'] = 'project_folder.config.settings.init'

How to configure wsgi application to migrate to django 1.7?

I'm trying to upgrade from Django 1.6 to 1.7.
When running python manage.py runserver, I got the following error :
django.core.exceptions.ImproperlyConfigured: WSGI application 'myapp.wsgi.application' could not be loaded; Error importing module: 'cannot import name get_path_info'
Here's the corresponding line in my settings.py :
WSGI_APPLICATION = 'myapp.wsgi.application'
Here's my wsgi.py file :
import os
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "myapp.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
from dj_static import Cling
application = Cling(get_wsgi_application())
Any idea to fix it ?
What version of dj_static are you using?
I upgraded to Django 1.7 and my site broke, leading me to your question.
I checked my dj_static version (I had been using 0.0.5 and noticed that the latest release at the time of this writing is 0.0.6).
Upon updating dj_static, my site appears to be functioning properly under Django 1.7.

Hosting Django app with Waitress

I'm trying to host a Django app on my Ubuntu VPS. I've got python, django, and waitress installed and the directories moved over.
I went to the Waitress site ( http://docs.pylonsproject.org/projects/waitress/en/latest/ ) and they said to use it like this:
from waitress import serve
serve(wsgiapp, host='5.5.5.5', port=8080)
Do I put my app name in place of of 'wsiapp'? Do I need to run this in the top-level Django project directory?
Tested with Django 1.9 and Waitress 0.9.0
You can use waitress with your django application by creating a script (e.g., server.py) in your django project root and importing the application variable from wsgi.py module:
yourdjangoproject project root structure
├── manage.py
├── server.py
├── yourdjangoproject
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
wsgi.py (Updated January 2021 w/ static serving)
This is the default django code for wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourdjangoproject.settings")
application = get_wsgi_application()
If you need static file serving, you can edit wsgi.py use something like whitenoise or dj-static for static assets:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourdjangoproject.settings")
"""
YOU ONLY NEED ONE OF THESE.
Choose middleware to serve static files.
WhiteNoise seems to be the go-to but I've used dj-static
successfully in many production applications.
"""
# If using WhiteNoise:
from whitenoise import WhiteNoise
application = WhiteNoise(get_wsgi_application())
# If using dj-static:
from dj_static import Cling
application = Cling(get_wsgi_application())
server.py
from waitress import serve
from yourdjangoproject.wsgi import application
if __name__ == '__main__':
serve(application, port='8000')
Usage
Now you can run $ python server.py
I managed to get it working by using a bash script instead of a python call. I made a script called 'startserver.sh' containing the following (replace yourprojectname with your project name obviously):
#!/bin/bash
waitress-serve --port=80 yourprojectname.wsgi:application
I put it in the top-level Django project directory.
Changed the permissions to execute by owner:
chmod 700 startserver.sh
Then I just execute the script on the server:
sudo ./startserver.sh
And that seemed to work just fine.

Categories