- Switching from python 2 to python 3 version.
- Installing pipenv using pip3.
- Installing dependencies using pipenv.
- Creating a starter project using django-admin.
- Running the basic migration with SQLite.
- Creating a super user account.
Python 2 will be no longer maintain until 2020, so its ideal if we switch to it's newer version you can read more about
- First we need to check the available versions of python.
ls /usr/bin/python*
It will display the python version's installed in your computer:
dev-mentor@devmentor-PC-MK34LEZCBEAD:~$ ls /usr/bin/python*
/usr/bin/python /usr/bin/python3 /usr/bin/python3.6m /usr/bin/python3.7m /usr/bin/python3m-config
/usr/bin/python2 /usr/bin/python3.6 /usr/bin/python3.6m-config /usr/bin/python3-config
/usr/bin/python2.7 /usr/bin/python3.6-config /usr/bin/python3.7 /usr/bin/python3m
if havent installed python 3 : Install-python-3-7-on-ubuntu-18-04.
- Next edit
./bashrc
.
sudo nano ~/.bashrc
- Set alias for
python
and point the version you may want in this casepython3.7
.
alias python='/usr/bin/python3.7'
- Reload the
.bashrc
.
source ~/.bashrc
- Finally we can check the new version.
dev-mentor@devmentor-PC-MK34LEZCBEAD:~$ python --version
Python 3.7.4
Python pip is a package manager for Python.
Installation:
sudo apt install python3-pip
#for python 2: sudo apt install python-pip
- Find the
pip3
path.
dev-mentor@devmentor-PC-MK34LEZCBEAD:~/Downloads/my-app$ whereis pip3
pip3: /usr/bin/pip3 /home/dev-mentor/.pyenv/shims/pip3.7 /home/dev-mentor/.pyenv/shims/pip3 /usr/share/man/man1/pip3.1.gz
- After that we need to update our
.bashrc
.
sudo nano ~/.bashrc
and set alias pip
by pointing its path.
alias pip='/usr/bin/pip3'
- Reload shell
source ~/.bashrc
- Done !
dev-mentor@devmentor-PC-MK34LEZCBEAD:~/Downloads/my-app$ pip --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.)
sudo pip3 install pipenv
#for python 2 version: sudo pip install pipenv
Check both versions.
dev-mentor@devmentor-PC-MK34LEZCBEAD:~$ pip --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
dev-mentor@devmentor-PC-MK34LEZCBEAD:~$ pipenv --version
pipenv, version 2018.11.26
- First we need to create our project container called
my-app
.
mkdir my-app && cd my-app
- We also need to initialize our virtual environment.
pipenv --three
- Install
python3
.
pipenv --python 3.7
- Install Django.
pipenv install Django==2.2.6
- Activate the virtual environment.
pipenv shell
Done:
(my-app) dev-100@dev100-PC-MK34LEZCBEAD:~/Downloads/my-app$
Pipfile Pipfile.lock
(my-app) = Our active virtual environment.
Pipfile = Handles the virtual environment packages and settings.
Our created Pipfile:
[[source]] # Here goes your package sources (where you are downloading your packages from).
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages] # The required development packages
[packages] # The required package to run application
django = "==2.2.6"
[requires] # The required python version
python_version = "3.6"
Example of what we have installed in (my-app)
environment:
Django Admin is a ready-to-use user interface for administrative activities. We all know how an admin interface is important for a web project. Django automatically generates admin UI based on your project models Read more.
- Since we installed
Django
, we can double check the version.
# If django-admin is not defined or shows with error you can install it by : pip install django
django-admin --version
- Initialize django project called
sample
.
django-admin startproject sample
We successfully created our sample
app folder structure.
(my-app) dev-mentor@devmentor-PC-MK34LEZCBEAD:~/Downloads/my-app$ tree
.
├── Pipfile
├── Pipfile.lock
└── sample
├── manage.py
└── sample
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
2 directories, 7 files
- Next to
sample
folder.
cd sample
- Try running our app.
python manage.py runserver
Output:
(my-app) dev-100@dev100-PC-MK34LEZCBEAD:~/Downloads/my-app$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 04, 2019 - 06:20:18
Django version 1.11.25, using settings 'test_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Host: http://127.0.0.1:8000/
ctrl+d
= Will exit (my-app) virtual environment.
ctrl+c
= Quit server.
By default Django comes with default database setting which is sqlite
, to create the tables type python manage.py migrate
.
(my-app) dev-100@dev100-PC-MK34LEZCBEAD:~/Downloads/my-app/sample$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
(my-app) dev-100@dev100-PC-MK34LEZCBEAD:~/Downloads/my-app/sample$
- After a successfull migration, you may want to create superuser that we will use to logged in into our admin dashboard.
python manage.py createsuperuser
django will ask some basic details for our superuser account.
(my-app) dev-100@dev100-PC-MK34LEZCBEAD:~/Downloads/my-app/sample$ python3 manage.py createsuperuser
Username (leave blank to use 'dev-100'): admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
- After that serve our application folder.
python manage.py runserver
- Next navigate to admin dashboard, you should now able to login as superuser account.
http://127.0.0.1:8000/admin