The following instructions are based on the official guide here.
pip install Django
django-admin startproject mysitepython manage.py runserverTo create an app:
python manage.py startapp pollsDatabase prepration:
# The migrate command looks at the INSTALLED_APPS setting
# and creates any necessary database tables
python manage.py migrate
# By running makemigrations, you’re telling Django that you’ve made
# some changes to your models and that you’d like the changes to
# be stored as a migration.
python manage.py makemigrations polls
# python manage.py sqlmigrate polls 0001
# The migrate command takes all the migrations that haven’t been applied
# and runs them against your database
python manage.py migrateAccess the site:
http://localhost:8000/polls
http://127.0.0.1:8000/admin
Create a superuser:
python manage.py createsuperuser- Write a view for
pollsapp - Config
urls.pyinpolls - Point the root URLconf (
mysite/urls.py) at thepolls.urlsmodule - Run
python manage.py migrate - Create models
- Include the app in the project by adding a reference to its configuration class in the
INSTALLED_APPS - you’ve made some changes to your models and that you’d like the changes to be stored as a migration:
python manage.py makemigrations polls - Create model tables in your database:
python manage.py migrate - Create an admin user
- Make the
pollsapp modifiable in the admin by editingpolls/admin.py - Write a view. A view serves a specific function and has a specific template
- Use class based views instead of function base views
python manage.py shellDjango REST framework is a powerful and flexible toolkit for building Web APIs. Helps with writing RESTful APIs and provides JSON parser, CRUD views, permissions, and serializers.
pip install djangorestframework
# Markdown support for the browsable API.
pip install markdown Once installed add rest_framework to the installed apps. Additionally in urls.py and settings.py:
path('api-auth/', include('rest_framework.urls')),REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
# No authentication required (test only)
# 'rest_framework.permissions.AllowAny'
]
}Then
- Define serializers
serializers.py - Define views
UserViewSetandGroupViewSet
pip install djangorestframework-simplejwtYou may follow the official guide page. The guide includes the following steps:
- Project Configuration (modify
settings.py) - In
urls.pyinclude routes for Simple JWT’s views - To protect each view, use
authentication_classeswithJWTAuthentication.