8

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?

Xhark
  • 781
  • 1
  • 9
  • 24
  • Have you read [this](https://channels.readthedocs.io/en/latest/deploying.html#run-protocol-servers)? And you don't need to use Django 3.0 to use channels by the way. – dirkgroten Feb 04 '20 at 13:14

2 Answers2

5

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()
Alper
  • 3,424
  • 4
  • 39
  • 45
  • 2
    It is worth mentioning that the module `django.core.asgi` does not exist in Django 2.2, the version mentioned in the question. – Ruben Alves Mar 11 '21 at 12:49
0

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()
Ruben Alves
  • 166
  • 4
  • 12