Python102 Curs3
Python102 Curs3
The manage.py script is used to interact with Django projects. It provides several commands like `runserver` to start a development server, `shell` for an interactive Python shell session in a Django environment, `makemigrations` for creating new migrations based on the changes detected to the models, and `migrate` for applying and unapplying migrations .
Django's ORM allows interaction with a database through Python code instead of SQL, providing an abstraction layer over databases. It enables developers to perform queries, insertions, updates, and deletions using model instances . This abstraction simplifies database interactions, minimizes potential SQL injection risks, and promotes database-agnostic application code, enhancing portability and maintainability .
Setting up a development environment for a Django project involves several steps: creating a Python virtual environment (`venv`), initializing a Django project, structuring directories (for templates, static files, and media), creating applications using `manage.py startapp`, and updating settings in `settings.py` . Additionally, developers may integrate APIs like those from `https://yts.mx` for data sourcing and prepare for production using external storage services for media files .
Relational fields in Django models define relationships between database tables. A `ForeignKey` establishes a many-to-one relationship, linking one model to another model. A `ManyToManyField` creates many-to-many relationships between models. These fields enhance the interaction and querying capabilities of models. For example, a `ForeignKey` could link a `Carbrand` model to a `Car` model, defining which brand manufactures each car .
In a Django project, `settings.py` contains server configurations, including applications, database type, system routes, and environment variables. It is central to configuring the project's overall behavior and functionalities . In contrast, `urls.py` maps URL patterns to their corresponding views. It links paths to view functions, determining how requests are handled by the application, which is crucial for defining navigation and access points .
Django implements an architecture closely related to MVC, known as MVT (Model-View-Template). The Model acts as an abstraction of the database table. The View executes logic and interacts with the Model to process data, rendering it using a Template. The Template is a presentation layer used for the user interface .
Django's admin interface serves as a configurable backend environment allowing administrators to perform various actions on database tables without direct database access. Developers can customize this interface by defining actions and models in `admin.py` and registering them using `admin.site.register()` . This flexibility enables tailored administration tasks specific to a project's requirements.
In Django, template rendering occurs when a View function receives a request and processes data using Models. It then utilizes a specific HTML template file found in the `templates` directory to map data into user-friendly content through context variables. The template files can use template tags to iteratively include data attributes, resulting in a dynamic, rendered HTML file .
Django offers several advantages including fast development, a loaded framework with built-in functionalities, high security, versatility, and high scalability . The framework allows developers to build web applications more quickly and with less code because it comes with patterns that can be selectively configured and even rewritten if necessary .
Using a virtual environment for a Django project is beneficial because it creates an isolated Python environment, separated from system dependencies and other projects. This isolation ensures that the project uses specific versions of packages without conflicts, enhancing manageability and reproducibility across different environments .