/ code

Python Sandbox: Django

On the sixth episode of season 3, we spoke about PyCharm and command line virtual environments using Python. If any of you are interested in that discussion, this post is for you!

Wanting to learn a new system from the ground up, building with a CLI seemed optimal. I fired up the terminal and installed the virtual environment necessary to get Django built out.

Note: I have an alias setup for Python —
alias python=python3
alias pip=pip3

I am using Python 3 for all of the following work. If you do not setup an alias and have both Python versions installed, use python3 and pip3.

$ mkdir -p Projects/django-site
$ cd Projects/django-site
$ python -m venv my-new_env
$ source my-new_env/bin/activate

The shell will change to have the environment before the path:

[(my-new_env)Projects/django-site]$ ls -la

I learned later that cloning this project on Windows required minor tweaks. A new virtual environment needed to be created and activation was slightly different:
PS P:\projects> python -m venv my-new_env

PS P:\projects> .\my-new_env\Scripts\activate

(my-new_env) PS P:\projects> ls

Once doing this, I copied all the files over, and the server fired up without issue.
It was time to install Django in the virtual environment and create the new project:

[(my-new_env)Projects/django-site]$ pip install Django
[(my-new_env)Projects/django-site]$ django-admin.py startproject newProject .

The dot is for simplicity purposes, creating a directory structure for ease of deployment.

I created a SQLite DB for managing authentication and user content:

[(my-new_env)Projects/django-site]$ 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 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 sessions.0001_initial... OK

manage.py was created with the project, it takes commands and runs them with the Django framework to get your project working.

With that, I ran:

[(my-new_env)Projects/django-site]$ python manage.py runserver

And I was off to the races:

django

I plan on continuing this endeavor. Django is a great framework to use with Python and I enjoy it. If you enjoyed this write up, stay tuned.

Python Sandbox: Django
Share this