Setup a New Django Project¶
Start a new Django project from scratch.
Create and Activate Virtual Environment¶
$ python -m venv .venv
$ source .venv/Scripts/activate
Install Django and Dependencies¶
Create a requirements.txt file. At this moment we put inside only django as the only requirement for the project:
django
Install Django as defined in requirements.txt:
$ pip install -r requirements.txt
...
Create Django Project¶
To create a new Django project:
$ django-admin startproject elearn
At this time you can already preview the project by running a development server:
$ python elear/manage.py migrate # Apply all pending database migrations
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 auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
$ python elearn/manage.py runserver # Start a development server
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 09, 2023 - 16:57:28
Django version 4.2.5, using settings 'elearn.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
You can open the browser and navigate to http://127.0.0.1:8000/ to see the default Django project page.
Start a New Django Application¶
To start a new Django application, use the Django command line inteface django-admin:
$ cd elearn
$ django-admin startapp courses
The new application needs to be registered in the project’s settings.py:
1# ...
2INSTALLED_APPS = [
3 # ...
4 # 3rd party apps
5 # Local apps
6 "courses",
7]
8# ...
We append the courses project in the INSTALLED_APPS list. The 3rd party apps and Local apps commented lines we keep to visually organize registered applications in three categories:
Django core applications
3rd party applications, e.g. Swagger API interface generator
Local applications which are part of our local Django project
Modify .gitignore File¶
Most likely you are using git as version control system. I recommend adding following lines to your .gitignore file:
1.vscode
2.dev
3.venv*
4# ...
Line #1 instructs git to ignore the Visual Studio Code metadata
Line #2 is something I like to use. I put random ad-hoc staff in a directory .dev which is not under version control
Line #3 instructs git to ignore our virtual environments
Create Superuser¶
$ python elearn/manage.py createsuperuser
Username (leave blank to use 'ivang'): ivang
Email address: ivang@home.local
Password:
Password (again):