Skip to content

Commit

Permalink
Update Django example
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jun 25, 2022
1 parent 4b69781 commit dc7ac74
Show file tree
Hide file tree
Showing 26 changed files with 121 additions and 129 deletions.
4 changes: 2 additions & 2 deletions examples/server/wsgi/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ time to the page.
This is an ideal application to measure the performance of the different
asynchronous modes supported by the Socket.IO server.

django_example
--------------
django_socketio
---------------

This is a version of the "app.py" application described above, that is based
on the Django web framework.
Expand Down
22 changes: 0 additions & 22 deletions examples/server/wsgi/django_example/django_example/urls.py

This file was deleted.

23 changes: 0 additions & 23 deletions examples/server/wsgi/django_example/manage.py

This file was deleted.

8 changes: 0 additions & 8 deletions examples/server/wsgi/django_example/requirements.txt

This file was deleted.

Empty file.

This file was deleted.

Empty file.
7 changes: 0 additions & 7 deletions examples/server/wsgi/django_example/socketio_app/urls.py

This file was deleted.

16 changes: 16 additions & 0 deletions examples/server/wsgi/django_socketio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
django-socketio
===============

This is an example Django application integrated with Socket.IO.

You can run it with the Django development web server:

```bash
python manage.py runserver
```

When running in this mode, you will see a warning indicating that the WebSocket
transport is not available, which is not supported by this web server.

See the documentation for information on supported deployment methods that you
can use to add support for WebSocket.
16 changes: 16 additions & 0 deletions examples/server/wsgi/django_socketio/django_socketio/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for django_socketio project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_socketio.settings')

application = get_asgi_application()
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
# flake8: noqa
"""
Django settings for django_example project.
Django settings for django_socketio project.
Generated by 'django-admin startproject' using Django 1.11.1.
Generated by 'django-admin startproject' using Django 4.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

import os
from pathlib import Path

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+vk#7#92ncb*y)8^$7sd&99%^+xc+t)nmamacbp8^vgjy(&g-9'
SECRET_KEY = 'django-insecure-&@-nkbrpe@%1_%ljh#oe@sw)6+k(&yn#r_)!5p)$22c^u#0@lj'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand All @@ -32,13 +31,13 @@
# Application definition

INSTALLED_APPS = [
'socketio_app',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'socketio_app',
]

MIDDLEWARE = [
Expand All @@ -51,7 +50,7 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'django_example.urls'
ROOT_URLCONF = 'django_socketio.urls'

TEMPLATES = [
{
Expand All @@ -69,22 +68,22 @@
},
]

WSGI_APPLICATION = 'django_example.wsgi.application'
WSGI_APPLICATION = 'django_socketio.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME': BASE_DIR / 'db.sqlite3',
}
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
Expand All @@ -103,20 +102,23 @@


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

STATIC_URL = '/static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
23 changes: 23 additions & 0 deletions examples/server/wsgi/django_socketio/django_socketio/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""django_socketio URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import include

urlpatterns = [
path('admin/', admin.site.urls),
path(r'', include('socketio_app.urls')),
]
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
WSGI config for django_example project.
WSGI config for django_socketio project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""

import os
Expand All @@ -14,7 +14,7 @@

from socketio_app.views import sio

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_example.settings")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_socketio.settings')

django_app = get_wsgi_application()
application = socketio.WSGIApp(sio, django_app)
22 changes: 22 additions & 0 deletions examples/server/wsgi/django_socketio/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_socketio.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
7 changes: 7 additions & 0 deletions examples/server/wsgi/django_socketio/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
asgiref==3.5.2
backports.zoneinfo==0.2.1
bidict==0.22.0
Django==4.0.5
python-engineio==4.3.2
python-socketio==5.6.0
sqlparse==0.4.2
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# flake8: noqa
from django.contrib import admin

# Register your models here.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@


class SocketioAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'socketio_app'
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# flake8: noqa
from django.db import models

# Create your models here.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# flake8: noqa
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions examples/server/wsgi/django_socketio/socketio_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from . import views

urlpatterns = [
path(r'', views.index, name='index'),
]
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ deps=
deps=
flake8
commands=
flake8 --exclude=".*" --ignore=W503,E402,E722 src/socketio tests examples
flake8 --exclude=".*" --exclude="examples/server/wsgi/django_socketio" --ignore=W503,E402,E722 src/socketio tests examples

[testenv:docs]
changedir=docs
Expand Down

0 comments on commit dc7ac74

Please sign in to comment.