Skip to content

Instantly share code, notes, and snippets.

@kalafut
Last active February 21, 2024 07:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kalafut/42bd31b2fdbf7a225da94e320d3e29ba to your computer and use it in GitHub Desktop.
Save kalafut/42bd31b2fdbf7a225da94e320d3e29ba to your computer and use it in GitHub Desktop.
Simple creation of a single-app django project
#!/bin/bash
#
# Simple creation of a single-app django project, as described in: https://zindilis.com/posts/django-anatomy-for-single-app/
#
# ./django_init foo
#
# This will result is the following flat structure:
#
# .
# └── foo
# ├── manage.py
# ├── settings.py
# ├── urls.py
# ├── foo
# │   ├── __init__.py
# │   ├── admin.py
# │   ├── apps.py
# │   ├── migrations
# │   │   └── __init__.py
# │   ├── models.py
# │   ├── tests.py
# │   └── views.py
# └── wsgi.py
#
# Note: this script assumes a GNU-compatible sed is installed. On macOS you can get this easily with Homebrew.
prj=$1
django-admin startproject $prj
cd $prj
mv $prj/* .
rm __init__.py
rm -rf $prj
sed -i 's/'"${prj}"'\.settings/settings/g' manage.py wsgi.py
sed -i 's/'"${prj}"'\.urls/urls/g' settings.py
sed -i 's/'"${prj}"'\.wsgi\.application/wsgi.application/g' settings.py
sed -i '/BASE_DIR = /s/Path(__file__).resolve().parent.parent/Path(__file__).resolve().parent/' settings.py
sed -i 's/'"${prj}"'\.wsgi\.application/wsgi.application/g' settings.py
./manage.py startapp ${prj}
@scottmacpherson
Copy link

Looks like the referenced blog post can now be found at https://zindilis.com/posts/django-anatomy-for-single-app/

@kalafut
Copy link
Author

kalafut commented Dec 29, 2020

TFW you get an email about a gist you completely forgot about 😅. Thanks, updated!

@kidpixo
Copy link

kidpixo commented Oct 19, 2021

Django ~3.1 uses pathlib instead os (yeeess!).

You should modify line 36 with

sed -i '/BASE_DIR = /s/Path(__file__).resolve().parent.parent/Path(__file__).resolve().parent/' settings.py

I forked and updated the your script here

thanks for the script!

@kalafut
Copy link
Author

kalafut commented Oct 19, 2021

@kidpixo Thanks! It has been a while since I've used Django (or python!), so I'll take your word for it and have updated the script as such.

@kidpixo
Copy link

kidpixo commented Oct 20, 2021

No problem, I tested on the latest django.

@tabcodes
Copy link

For anyone coming across this now, creating a new project still works with the latest version of Django.

Thank you for investigating this- I have had a hellacious time organizing a single-app project of mild-to-moderate complexity, and while it already existed, so the script didn't come in handy, the organization you've pointed out above has worked wonders for me in dev & prod.

@kalafut
Copy link
Author

kalafut commented Nov 27, 2022

Thanks for the verification!

@Fingel
Copy link

Fingel commented Jan 26, 2024

It should be noted that you still need to add ${prj} to INSTALLED_APPS for things like models to be detected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment