Django Basics
What is Django?
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the Model-View-Template (MVT) architectural pattern and provides many built-in tools and features to make web development faster and easier, such as an ORM, authentication, and admin panel.
What are the main features of Django?
Django provides many built-in features that make it a popular choice for web development. Some key features include:
- ORM (Object-Relational Mapping): A powerful ORM that allows developers to interact with databases using Python code instead of SQL.
- Admin Interface: An automatically generated admin panel for managing application data.
- Authentication: A built-in system for handling user authentication, including login, logout, password management, and permissions.
- Security: Django comes with features to protect against common web vulnerabilities, such as XSS, CSRF, and SQL injection.
- Scalability: Django is designed to scale and can handle large amounts of traffic.
- Templating System: A templating engine to dynamically generate HTML content.
What is the Django MVT architecture?
Django follows the Model-View-Template (MVT) architectural pattern:
- Model: Represents the data layer and defines the structure of the database. It includes the ORM, which allows developers to interact with the database using Python code.
- View: The logic layer, where the application processes requests, interacts with models, and returns the appropriate response (usually in the form of HTML).
- Template: The presentation layer responsible for defining how data is presented to the user in an HTML format.
How do you create a new Django project?
To create a new Django project, you use the django-admin command to generate the initial project structure. This command creates the necessary directories and files for your Django project.
Example of creating a new project:
django-admin startproject myprojectIn this example, a new project called myproject is created, and it includes the project's settings, URLs, and WSGI configuration.
How do you run a Django development server?
Once you've created a Django project, you can run a local development server using the manage.py script. The development server allows you to test your application locally.
Example of running the development server:
python manage.py runserver
By default, the server runs on http://127.0.0.1:8000/. You can specify a different port or IP address if needed.
What is the purpose of the manage.py file in Django?
The manage.py file is a command-line utility that allows you to interact with your Django project. It provides various commands for tasks like running the development server, applying migrations, creating apps, and managing users.
Example of using manage.py to create an app:
python manage.py startapp myapp
In this example, a new app called myapp is created within the project.
What are apps in Django?
An app in Django is a web application that does something specific, such as handling blog posts, user authentication, or a shopping cart. A Django project can have multiple apps, and each app serves a distinct purpose. Apps are self-contained and reusable across projects.
Example of creating an app:
python manage.py startapp blog
In this example, a new app called blog is created within the project, and it can be configured in the project's settings file.
How do you configure a Django project's settings?
Django projects have a settings.py file that contains all the configuration options for the project, such as database settings, middleware, installed apps, and templates. You can modify this file to customize your project's behavior.
Example of setting the database configuration in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
In this example, the project is configured to use an SQLite database.
What are migrations in Django?
Migrations in Django are a way to propagate changes made to models (such as adding fields or modifying the database schema) into the database. Migrations are created automatically when you make changes to your models, and they can be applied to the database using the migrate command.
Example of applying migrations:
python manage.py migrate
This command applies any pending migrations to the database, keeping it synchronized with your models.
How do you create and apply migrations in Django?
To create a migration after modifying a model, you use the makemigrations command. This generates the migration files needed to apply the changes to the database. After creating the migration, you use the migrate command to apply it.
Example of creating and applying a migration:
python manage.py makemigrations
python manage.py migrate
In this example, Django detects changes in the models and generates a migration file. Then, the migration is applied to the database.
What is Django's admin panel?
Django's admin panel is a built-in interface for managing and interacting with your application's models. It provides a user-friendly interface to add, update, and delete data directly from the browser. The admin panel is automatically generated based on your models and can be customized extensively.
Example of accessing the admin panel:
http://127.0.0.1:8000/admin/
You can access the admin panel by visiting /admin/ after running the development server.
How do you enable an app in Django's admin panel?
To enable an app in Django's admin panel, you need to register the app's models in the admin.py file of the app. This tells Django to include the app's models in the admin interface, allowing you to manage them through the admin panel.
Example of registering a model in the admin panel:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
In this example, the Post model is registered in the admin panel, allowing it to be managed from the admin interface.
How do you create a superuser in Django?
A superuser is a user with all permissions in the Django admin panel. To create a superuser, you use the createsuperuser command, which prompts you for a username, email, and password.
Example of creating a superuser:
python manage.py createsuperuser
Once the superuser is created, you can log in to the admin panel and manage the site's data.
What is the purpose of the urls.py file in Django?
The urls.py file in Django is responsible for defining URL patterns and routing incoming requests to the appropriate views. You define URL routes in this file, and when a request matches a route, the corresponding view is called to handle the request.
Example of a URL pattern:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
In this example, the index view is called when the user accesses the root URL.