Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc.
Setup
- Install Django version 3.0.1:
- Check Django version:
- Create a new Django project:
- Navigate into the project directory:
- Create a new app within the project:
- Start the development server:
- To stop the server, use
ctrl+z
.
sudo pip install django==3.0.1
python -m django --version
django-admin startproject project_name
cd project_name
python manage.py startapp app_name
python manage.py runserver
Database Migrations
- Create initial migrations:
- Apply migrations:
python manage.py makemigrations
python manage.py migrate
This applies migrations for built-in models like admin, auth, contenttypes, and sessions.
Exploring Models
To see the default models available in your project:
- Open the Django shell:
- Run the following commands:
python manage.py shell
from django.apps import apps
for app in apps.get_app_configs():
models = app.get_models()
for model in models:
print(model.__name__)
Viewing Model Data
To see the data in a model:
- Open the Django shell:
- Import the necessary model:
- Retrieve and print user data:
python manage.py shell
from django.contrib.auth.models import User
users = User.objects.all()
for user in users:
print(user.username)
Comments
Post a Comment