Skip to content

Instantly share code, notes, and snippets.

@codyeatworld
Last active April 6, 2018 17:46
Show Gist options
  • Save codyeatworld/eef63d2d2712045faf5149cddcb37b9e to your computer and use it in GitHub Desktop.
Save codyeatworld/eef63d2d2712045faf5149cddcb37b9e to your computer and use it in GitHub Desktop.
Generate django projects
# Usage:
# djgen <project_name> <app_name>
# Parameters:
# $1 <project_name>
# $2 <app_name>
djgen() {
echo "πŸ‘Ÿ [Running] django-admin startproject $1"
django-admin startproject $1
cd $1
echo "πŸ‘Ÿ [Running] mkdir apps"
mkdir apps
echo "πŸ‘Ÿ [Running] touch apps/__init__.py"
touch apps/__init__.py
cd apps
echo "πŸ‘Ÿ [Running] python ../manage.py startapp $2."
python ../manage.py startapp $2
cd ../
echo "πŸ‘Ÿ [Running] mkdir -p apps/$2/templates/$2"
mkdir -p apps/$2/templates/$2
echo "πŸ‘Ÿ [Running] touch apps/$2/templates/$2/index.html"
touch apps/$2/templates/$2/index.html
echo "✨ [Generating] apps/$2/templates/$2/index.html"
echo "<h1>Hi</h1>" >> apps/$2/templates/$2/index.html
echo "✨ [Generating] apps/$2/urls.py"
touch apps/$2/urls.py
echo "from django.conf.urls import url" >> apps/$2/urls.py
echo "from . import views" >> apps/$2/urls.py
echo "" >> apps/$2/urls.py
echo "urlpatterns = [" >> apps/$2/urls.py
echo " url(r'^', views.index)," >> apps/$2/urls.py
echo "]" >> apps/$2/urls.py
cd $1
echo "βœ‚οΈ [Modifying] $1/urls.py"
gsed -i '16s/$/, include/' urls.py
gsed -i "20i\ url(r'^', include('apps.$2.urls'))," urls.py
echo "βœ‚οΈ [Modifying] $1/settings.py"
gsed -i "34i\ 'apps.$2'," settings.py
cd ../
echo "βœ‚οΈ [Modifying] apps/$2/views.py"
echo "def index(req):" >> apps/$2/views.py
echo " return render(req, '$2/index.html')" >> apps/$2/views.py
cd ../
touch $1/apps/$2/views.py
touch $1/apps/$2/urls.py
touch $1/apps/$2/templates/$2/index.html
echo "" >> $1/$1/settings.py
touch $1/$1/urls.py
echo "πŸ‘Ÿ [Migrating] python manage.py migrate"
cd $1
python manage.py migrate
cd ../
echo "Done!"
}
# pyenv() {
# source ~/codingdojo/python/envs/$1/bin/activate
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment