Setup and create project with Django
- Set up virtualenv
- venv
- Install django
- pip install "Django==3.0.*"
- Check it has been installed
- python; import django; django.get_version()
- Start new application
- django-admin startproject site
- view directory structure
- manage.py is the cli tool used to interact with the project
- site/ is the project directory
- init.py empty to let Python treat this directory as a module
- asgi.py - configuration for ASGI which is a stanard for async web servers and applications
- settings.py - the settings and config
- urls.py the routing
- wsgi.py - web server gateway interface configuration
- python manage.py migrate
- python manage.py runserver
- Setup an app
- Setup app
-
python manage.py startapp blog
-
These files are as follows:
- admin.py: This is where you register models to include them in the Django administration site—using this site is optional.
- apps.py: This includes the main configuration of the blog application.
- migrations: This directory will contain database migrations of your application. Migrations allow Django to track your model changes and synchronize the database accordingly.
- models.py: This includes the data models of your application; all Django applications need to have a models.py file, but this file can be left empty.
- tests.py: This is where you can add tests for your application.
- views.py: The logic of your application goes here; each view receives an HTTP request, processes it, and returns a response.
- Add the blog post model
-
Add the applications to the installed_apps
-
Make the migrations
-
python manage.py makemigrations blog
-
View the SQL commands
-
python manage.py sqlmigrate blog 0001
-
Run the migrations
-
python manage.py migrate
- Creating admin site for the models
- Create a superuser
- Look at the admin