Django getting started
Create project scaffolding
- Create the project folder and initialization the virtual environment
1
2# Create the project directory
3mkdir djangoApp
4cd djangoApp
5
6# Initialization the virtual environment
7python -m venv ~/.venv/global
8source ~/.venv/global/bin/activate
9
10# Install the Django framework
11pip install django
shell
- Create the project
1mkdir myDjangoProject
2django-admin startproject mysite myDjangoProject
3cd myDjangoProject
4# create the app in project
5python manage.py startapp polls
6
7# can create another app in this project
8python manage.py startapp otherapp
shell
- Apply the databases migrations
1# generate the migrations in migrations directory
2python manage.py makemigrations
3# or apply some single app migration
4python manage.py makemigrations polls
5
6# view one migrate sql
7# The sqlmigrate command doesn’t actually run the migration on your database
8python manage.py sqlmigrate polls 0001
9
10# apply the migrations
11python manage.py migrate
shell
- Run local development server
Use the api in python shell
1python manage.py shell
2
3from polls.models import Choice, Question
4q = Question(question_text="What's new?", pub_date=timezone.now())
5q.save()
6q.id
7q.question_text
8q.pub_date
9q.question_text = "What's up?"
10q.save()
11
12Question.objects.all()
shell