-
-
Save lgarciasbr/b1a19a1984558043a46d to your computer and use it in GitHub Desktop.
Shell script to create a simple Django project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Shell script for create a simple Django project. | |
| # wget --output-document=setup.sh https://goo.gl/3j87iT | |
| # Type the following command, you can change the project name. | |
| # source setup.sh myproject | |
| # Colors | |
| red=`tput setaf 1` | |
| green=`tput setaf 2` | |
| reset=`tput sgr0` | |
| PROJECT=${1-myproject} | |
| echo "${green}>>> Remove .venv${reset}" | |
| rm -rf .venv | |
| echo "${green}>>> Remove djangoproject${reset}" | |
| rm -rf djangoproject | |
| echo "${green}>>> Creating virtualenv${reset}" | |
| virtualenv -p python3 .venv | |
| echo "${green}>>> .venv is created.${reset}" | |
| # active | |
| sleep 2 | |
| echo "${green}>>> activate the .venv.${reset}" | |
| source .venv/bin/activate | |
| PS1="(`basename \"$VIRTUAL_ENV\"`)\e[1;34m:/\W\033[00m$ " | |
| sleep 2 | |
| echo "${green}>>> Creating djangoproject.${reset}" | |
| mkdir djangoproject | |
| cd djangoproject | |
| # installdjango | |
| echo "${green}>>> Installing the Django${reset}" | |
| pip install django | |
| pip freeze > requirements.txt | |
| # createproject | |
| echo "${green}>>> Creating the project '$PROJECT' ...${reset}" | |
| django-admin.py startproject $PROJECT . | |
| cd $PROJECT | |
| echo "${green}>>> Creating the app 'core' ...${reset}" | |
| python ../manage.py startapp core | |
| cd .. | |
| # migrate | |
| python manage.py makemigrations | |
| python manage.py migrate | |
| # createuser | |
| echo "${green}>>> Creating a 'admin' user ...${reset}" | |
| echo "${green}>>> The password must contain at least 8 characters.${reset}" | |
| echo "${green}>>> Password suggestions: djangoadmin${reset}" | |
| python manage.py createsuperuser --username='admin' --email='' | |
| # ********** MAGIC ********** | |
| echo "Editing settings.py" | |
| sed -i "/django.contrib.staticfiles/a\ '$PROJECT.core'," $PROJECT/settings.py | |
| echo "Editing urls.py" | |
| # exclude 16 lines by urls.py | |
| sed -i "1,16d" $PROJECT/urls.py | |
| # insert text in urls.py | |
| sed -i "/import admin/a\import $PROJECT.core.views as v" $PROJECT/urls.py | |
| # the \x24 is $ ascii character | |
| sed -i "/urlpatterns = \[/a\ url(r'\^\x24\', v.home, name='home')," $PROJECT/urls.py | |
| echo "Create the view more simple" | |
| # exclude line 2 by views.py | |
| sed -i "2d" $PROJECT/core/views.py | |
| sed -i "2c\from django.http import HttpResponse\n\n\ndef home(request):\n return HttpResponse('<h1>Welcome to the Django.</h1>')" $PROJECT/core/views.py | |
| # ********** END OF THE MAGIC ********** | |
| sleep 2 | |
| echo "${green}>>> Done${reset}" | |
| sleep 2 | |
| # run | |
| python manage.py runserver | |
| # https://www.gnu.org/software/sed/manual/sed.html | |
| # http://www.asciitable.com/ | |
| # http://linuxconfig.org/add-character-to-the-beginning-of-each-line-using-sed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment