I have an existent django project in 2.2, but now i would like to start using channels, so I have to change to 3.0 and asgi instead of wsgi.
How can I generate the asgi.py that I need to run the app?
Django has a template file here that it uses to generate the asgi.py.
Jus copy paste this next to your wsgi.py:
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')
application = get_asgi_application()
For Django==2.2, I found the solution in this link. For the asgi.py file, the content would be:
"""
ASGI entrypoint. Configures Django and then runs the application
defined in the ASGI_APPLICATION setting.
"""
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
django.setup()
application = get_default_application()
Related
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
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'
So I wanted to deploy my first django application on a cherryPy webserver using wsgi. And I have issues with os.environ['DJANGO_SETTINGS_MODULE']. When trying to run application callable it throws error, that module is not found. Project structure:
ResourceManager
ResourceManager
ResourceManager
__init__.py
cherryserver.py
settings.py
urls.py
wsgi.py
SimpleResourceManager
migrations
__init__.py
admin.py
apps.py
models.py
serializers.py
tests.py
urls.py
views.py
manage.py
wsgi.py file:
import os
import sys
from django.core.wsgi import get_wsgi_application
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'ResourceManager.settings'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ResourceManager.settings')
application = get_wsgi_application()
cherryserver.py:
import cherrypy
from ResourceManager.ResourceManager.wsgi import application
if __name__ == '__main__':
# Mount the application
cherrypy.tree.graft(application, "/")
# Unsubscribe the default server
cherrypy.server.unsubscribe()
# Instantiate a new server object
server = cherrypy._cpserver.Server()
# Configure the server object
server.socket_host = "0.0.0.0"
server.socket_port = 8080
server.thread_pool = 30
# Subscribe this server
server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
Application works fine when using command runserver 8080, but when i tried to run it on different server. It says ModuleNotFoundError: No module named "ResourceManager.settings".
So i tried: Change where cherryserver.py is located in directory, I have added additional lines of code to wsgy.py file and I'm running out of ideas what is wrong when I'm deploying my app on different server. Why I'm using cherryPy, well I have to test 5 web servers that are based on python.
Try changing from:
from ResourceManager.ResourceManager.wsgi import application
To:
from wsgi.py import application
This is because they are located in the same directory, if this does not work just mess around with the from as it seems that your path to the wsgi file is wrong.
I am reading the python-socketio example.
in its wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
from socketio import Middleware
from socketio_app.views import sio
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_example.settings")
django_app = get_wsgi_application()
application = Middleware(sio, django_app)
in the views.py:
async_mode = None
import os
from django.http import HttpResponse
import socketio
basedir = os.path.dirname(os.path.realpath(__file__))
sio = socketio.Server(async_mode=async_mode)
thread = None
I want to know, the GitHub example shows use this method to register wsgi application:
django_app = get_wsgi_application()
application = Middleware(sio, django_app)
But in my project, the python-socketio is a part of it, how can I register the wsgi in this scenario?
I mean I want to make the python-socketio application to be a part of my project, as a app in project. But I don't know how to configure the wsgi.
EDIT-01
My project wsgi.py current code is bellow:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Qiyun02.settings")
application = get_wsgi_application()
Finally, I figure out the way to configure it:
in the wsgi.py, configure the eventlet like bellow, I solve this issue.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Qyun.settings")
from socketio import Middleware
from website_chat.views import sio
django_app = get_wsgi_application()
application = Middleware(sio, django_app)
import eventlet
import eventlet.wsgi
eventlet.wsgi.server(eventlet.listen(('', 8000)), application)
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.