Using daphne here's my setup:
PROCFILE:
web: daphne my_application.asgi:application --port $PORT --bind 0.0.0.0 -v2
SETTINGS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'channels',
'django_summernote',
....
]
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
}
}
}
ASGI_APPLICATION = "my_application.routing.application"
ROUTING FILE:
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.conf.urls import url
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter(
[
url(*),
....
]
)
),
})
ASGI.PY - WHERE THE ERROR OCCURS
"""
ASGI entrypoint. Configures Django and then runs the application
defined in the ASGI_APPLICATION setting.
"""
import os
import django
# HERE IT THROWS THE IMPORT ERROR
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_application.settings")
django.setup()
application = get_default_application()
REQUIREMENTS:
...
channels
channels_redis
...
So, using the latest 2.(2?) package I believe which I just verified has the source code required.
IMPORT ERROR
from channels.exceptions import RequestAborted, RequestTimeout
ImportError: cannot import name 'RequestAborted'
I clearly have the right package and this is available per the source code so wtf is going on here.....?
As #chander mentioned: problem is that you install django-channels, when you were already having channels.
to solve problem run:
pip uninstall django-channels
pip uninstall channels
pip install channels
I'm guessing that you did a:
pip install django-channels
whereas you should have done
pip install channels
To install the latest version of channels.
I had a similar problem - in the end I blew away my venv and pyenv and reinstalled - which fixed the issue. The problem started when I accidentally installed django-channels when I already had channels installed.
Related
I am attempting Django Channels for the first time. I am following this tutorial - https://www.youtube.com/watch?v=cw8-KFVXpTE&t=21s - which basically explains Channels basics. I installed Channels in my virual environment using pip install channels and it installed the latest version, which is 4.0.0. I have laid out the basic setup. But when I run python manage.py runserver, I get -
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
when I shoul be getting this -
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting ASGI/Channels version 4.0.0 development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
I cannot figure out what I might be doing wrong here and I cannot find any relevant solution anywhere. I would really appreciate any suggestions anyone might have.
settings.py
INSTALLED_APPS = [
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'Orders',
]
ASGI_APPLICATION = "BrokerBackEnd.asgi.application"
asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
import Orders.routing as route
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BrokerBackEnd.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': URLRouter(
route.websocket_urlpatterns
)
})
consumer.py
class OrderConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def receive(self, text_data=None, bytes_data=None):
self.send(text_data="Message = " + text_data)
def disconnect(self, code):
return super().disconnect(code)
routing.py
websocket_urlpatterns = [
re_path(r"socket/order/", OrderConsumer.as_asgi())
]
As of django-channels 4.0.0 the daphne server was decoupled from the rest of channels. Now, you need to include daphne in your settings.py INSTALLED_APPS if you wish to use it. Include this at the top of your installed apps. The tutorial you are looking at uses an older version of channels.
INSTALLED_APPS = [
'daphne',
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'Orders',
]
ASGI_APPLICATION = "BrokerBackEnd.asgi.application"
Also, make sure daphne is actually installed:
pip install -U channels["daphne"]
I am trying to deploy my app to heroku and I keep getting this error even though when I run locally it works perfectly fine. I have added django-pwa==1.0.10 to my requirments.txt file also so that heroku installs the package. Here are my installed apps in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pwa',
# 'embed_video',
'whitenoise.runserver_nostatic',
'courses.apps.CoursesConfig',
'accounts.apps.AccountsConfig',
'announcements.apps.AnnouncementsConfig',
'students.apps.StudentsConfig',
'home.apps.HomeConfig',
'event_calendar.apps.EventCalendarConfig',
]
Here is my directory
Not really sure what to do as I am not very experienced with heroku.
By default Heroku will try to install every app from your requirements.txt, so before going any further make sure of the following:
You have run pip freeze > requirements.txtto reflect the change
Your path for the Procfile is correct
If it does not work after the troubleshooting, add your log from heroku to your first question, your procfile and requirements.txt.
Learning about procfile
I have used swagger to document my APIs on the Django rest framework, and deploy it on Pythonanywhere.
but the documentation URL doesn't have any response.
it is my urls.py code for swagger:
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="BoardGame API",
default_version='v1',
description="Test description",
terms_of_service="http://****.pythonanywhere.com/",
contact=openapi.Contact(email="contact#boardgame.local"),
license=openapi.License(name="TEST License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
...
path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]
and my settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'drf_yasg',
'rest_framework_swagger',
'rest_framework_simplejwt',
'django_extensions',
...
]
Its because your static files has not been loaded on server try to collectstatic files and check in inspect of page whether css is loading or not
You have to learn about static in Django
When you apply it all UI related files will be created in a static folder
Then swagger UI will work on pythonanywhere
It's seems that static file is not loaded on live server:
step 1:
LOCAL= False
step 2:
python manage.py collectstatic
step 3:
Reset server
nginx: sudo systemctl restart nginx
gunicorn: sudo systemctl restart gunicorn
I'm trying to use channels for a django app.I have installed all the required dependencies (i think). I have listed 'channels' on INSTALLED_APPS of myapp/settings.py.However,I run daphne ( daphne chat.asgi:channel_layer --port 8888)-( no error message on cmd), then when i run python manage.py runworker which gives an Error message that says - "channels.asgi.InvalidChannelLayerError: no BACKEND specified for default". . I'm novice for django, i have asgi.py as
import os
import channels.asgi
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chat.settings")
channel_layer = channels.asgi.get_channel_layer()
But in my myapp/settings.py, i have specified the BACKEND specified for default.Can you please suggest a solution to this error? Here is a probable solution,but the asgi_redis was current in my django1.10. I'm trying to run myapp on my local machine.
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
#"hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
},
"ROUTING": "myproject.myapp.routing.channel_routing",
},
}
Add this to the top of your settings.py
import asgi_redis
Also, make sure that you have installed Redis
pip install asgi_redis
I'm not able to figure out what's going wrong. I'm using pycharm and bitnami django stack for developing my first web application.
Here is my directory structure:
project name: myapp
location: C:\Bitnami\djangostack-1.7.8-0\apache2\htdocs\myapp
Directory structure:
myapp
manage.py
app
admin.py
models.py
settings.py
tests.py
urls.py
views.py
wsgi.py
migrations
templates
home.html
my settings.py has following data:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
)
ROOT_URLCONF = 'app.urls'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
my urls.py has following data:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'myapp/', views.homepage, name='home'),
]
my views.py has following data:
from django.http import HttpResponse
def homepage(request):
return HttpResponse("Hello, world!")
Now when I try to run:
http://localhost/myapp
It simply displays the list of files in the myapp directory
I'm not able to find why it is not executing from urls.py
If you want to get your app up and running with Apache, you will have to enable reverse-proxying.
To do this, you can try adding the following lines to /opt/bitnami/apache2/conf/httpd.conf, supposing that your application myapp is running at port 8000:
ProxyPass /myapp http://localhost:8000/myapp
ProxyPassReverse /myapp http://localhost:8000/myapp
To make sure that the mod_proxy module is enabled, please find the following lines and uncomment them:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
Finally, after saving the changes you can proceed to restart Apache:
/opt/bitnami/ctlscript.sh restart apache
You should now be able to access your application in: http://localhost/myapp
I'm able to run the project with the development server. In Pycharm, I went to Tools -> Run manage.py Task -> run server
And then executed the url:
http://localhost:8000/myapp/
But still trying to figure out why it is not running with apache in Bitnami stack