Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/python/django/manage.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions examples/python/django/myapp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file can be left empty
7 changes: 7 additions & 0 deletions examples/python/django/myapp/models.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions examples/python/django/myapp/views.py
Original file line number Diff line number Diff line change
@@ -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.")
Empty file.
16 changes: 16 additions & 0 deletions examples/python/django/myproject/asgi.py
Original file line number Diff line number Diff line change
@@ -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()
121 changes: 121 additions & 0 deletions examples/python/django/myproject/settings.py
Original file line number Diff line number Diff line change
@@ -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/'
10 changes: 10 additions & 0 deletions examples/python/django/myproject/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
]
16 changes: 16 additions & 0 deletions examples/python/django/myproject/wsgi.py
Original file line number Diff line number Diff line change
@@ -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()