Django - useful commands
Django framework is based on python and works with python.
Django and required modules are installed using pip.
Upgrading pip:
python -m pip install --upgrade pip
pip install django
install specific version of django:
pip install django==4.2
verify django installation:
django-admin --version
Django has Projects and with in projects contains one or more apps.
Creating project (django-admin used to create project)
django-admin startproject <project_name>
note: using underscore "_"
while creating django project django-admin creates a manage.py along with other required files and folders in the project directory. Some times create an db.sqlite3 as well.
the manage.py used further to manage the django project and applications.
python manage.py startapp <appname>
Once the app is created it needs to be added to the settings.py file inside the project folder, under the INSTALLED_APPS array.
Starting the development server,
python manage.py runserver
note django uses 8000 as default port
To run on different port
python manage.py runserver 8080
The service can be accessed from the below URLs in web browser,
http://localhost:8000/ (or) http://127.0.0.1:8000/
A typical django project folder contain following files:
__init__.py
settings.py
urls.py (for url routing)
asgi.py (asynchronous server gateway interface)
wsgi.py (web server gateway interface)
A typical django app contain the following files:
__init__.py
admin.py
apps.py
migrations\ (directory)
models.py
views.py
tests.py
static\ (directory)
templates\ (directory)
For REST API,
pip install django-rest-framework
pip install django-cors-headers
add the following to the INSTALLED_APPS of settings.py in project folder:
"rest_framework"
"corsheaders"
also need to understand and create the following for REST API,
Serializers (may need to create "serializers.py")
Viewsets (to be written inside views.py of the app)
Queryset
urls.py (inside app folder as well in the project folder)
Routing (routers.py)
Comments
Post a Comment