Skip to content

Instantly share code, notes, and snippets.

@MrYakobo
Created June 5, 2025 21:54
Show Gist options
  • Save MrYakobo/a4d308653d54533a351ccc83509f2b24 to your computer and use it in GitHub Desktop.
Save MrYakobo/a4d308653d54533a351ccc83509f2b24 to your computer and use it in GitHub Desktop.
a very fresh take on django, using a single file for everything
#!/usr/bin/env python3
# DJANGO_DB=database.sqlite python3 django_singlefile.py runserver
from django.conf import settings
from django.core.management import execute_from_command_line
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import path
from django.db import models
from django import forms
from django.shortcuts import render
from django.core.wsgi import get_wsgi_application
BASE_DIR = os.path.dirname(__file__)
settings.configure(
DEBUG=True,
SECRET_KEY='insecure',
ROOT_URLCONF=__name__,
ALLOWED_HOSTS=['*'],
INSTALLED_APPS=['django.contrib.contenttypes', __name__],
MIDDLEWARE=[],
DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.environ['DJANGO_DB']}},
TEMPLATES=[{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': False,
'OPTIONS': {
'loaders': [
('django.template.loaders.locmem.Loader', {
'index.html': """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body>
<main class="prose max-w-lg text-center mx-auto my-8">
<h1>my sexy django app</h1>
</main>
</body>
</html>
"""
})
]
}
}]
)
import django
django.setup()
class Person(models.Model):
name = models.CharField()
email = models.EmailField()
phone = models.CharField()
class NameForm(forms.Form):
name = forms.CharField()
email = forms.EmailField(required=False)
def index(request):
form = NameForm()
return render(request, "index.html", {"form": form, "message": message})
urlpatterns = [path('', index)]
apps_ready = False
def main():
global apps_ready
if not apps_ready:
import django
django.setup()
from django.core.management import call_command
call_command('makemigrations', __name__)
call_command('migrate')
apps_ready = True
execute_from_command_line(sys.argv)
application = get_wsgi_application()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment