diff --git a/examples/python/django/manage.py b/examples/python/django/manage.py new file mode 100644 index 0000000..fbf0a91 --- /dev/null +++ b/examples/python/django/manage.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.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) diff --git a/examples/python/django/myapp/__init__.py b/examples/python/django/myapp/__init__.py new file mode 100644 index 0000000..dd0fede --- /dev/null +++ b/examples/python/django/myapp/__init__.py @@ -0,0 +1 @@ +# This file can be left empty \ No newline at end of file diff --git a/examples/python/django/myapp/models.py b/examples/python/django/myapp/models.py new file mode 100644 index 0000000..085d69f --- /dev/null +++ b/examples/python/django/myapp/models.py @@ -0,0 +1,7 @@ +from django.db import models + +class MyModel(models.Model): + name = models.CharField(max_length=100) + + def __str__(self): + return self.name diff --git a/examples/python/django/myapp/views.py b/examples/python/django/myapp/views.py new file mode 100644 index 0000000..4dbc86c --- /dev/null +++ b/examples/python/django/myapp/views.py @@ -0,0 +1,23 @@ +from django.shortcuts import render +from django.http import HttpResponse +from django.db import connection +from .models import MyModel + +def my_view(request): + user_id = request.GET.get('user_id') + with connection.cursor() as cursor: + cursor.execute(f"SELECT * FROM myapp_mymodel WHERE id = {user_id}") + row = cursor.fetchone() + + user_name = request.GET.get('user_name') + + return HttpResponse(f"Hello, {user_name}") + +def index(request): + return HttpResponse("Hello, world. Youre at the polls index.") # Typo: You're -> Youre + +def my_view2(request): + value = request.GET.get('value') + if value and value == '0': + return HttpResponse("Value is zero.") + return HttpResponse("Value is not zero.") diff --git a/examples/python/django/myproject/__init__.py b/examples/python/django/myproject/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/python/django/myproject/asgi.py b/examples/python/django/myproject/asgi.py new file mode 100644 index 0000000..3b14b53 --- /dev/null +++ b/examples/python/django/myproject/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for myproject 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/3.x/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') + +application = get_asgi_application() diff --git a/examples/python/django/myproject/settings.py b/examples/python/django/myproject/settings.py new file mode 100644 index 0000000..7b2d1be --- /dev/null +++ b/examples/python/django/myproject/settings.py @@ -0,0 +1,121 @@ +""" +Django settings for myproject project. + +Generated by 'django-admin startproject' using Django 3.x. + +For more information on this file, see +https://docs.djangoproject.com/en/3.x/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.x/ref/settings/ +""" + +from pathlib import Path + +# 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/3.x/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '247r6tb3ifgn3rvw74i6tvni3476ftbifg3r' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'myapp', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'myproject.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'myproject.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.x/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.x/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.x/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/3.x/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/examples/python/django/myproject/urls.py b/examples/python/django/myproject/urls.py new file mode 100644 index 0000000..7173301 --- /dev/null +++ b/examples/python/django/myproject/urls.py @@ -0,0 +1,10 @@ +from django.contrib import admin +from django.urls import path +from myapp import views + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', views.index, name='index'), + path('my_view/', views.my_view, name='my_view'), + path('my_view2/', views.my_view2, name='my_view2'), +] diff --git a/examples/python/django/myproject/wsgi.py b/examples/python/django/myproject/wsgi.py new file mode 100644 index 0000000..c9ae45d --- /dev/null +++ b/examples/python/django/myproject/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for myproject 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/3.x/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') + +application = get_wsgi_application()