Lädt...


🔧 Django | Project Structure


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Django's default project structure organizes files and folders to help developers manage code efficiently. Here's a breakdown of the typical file structure when you create a new Django project:

  1. Project Root Folder

When you start a new Django project, it generates a root folder named after the project (e.g., my_project/). This folder contains the overall project files.

  1. manage.py

This script acts as a command-line utility for interacting with your Django project. You can run commands like starting the development server, making migrations, creating superusers, and more.

Usage:

python manage.py runserver
python manage.py migrate
python manage.py createsuperuser

  1. Project Subdirectory (my_project/)

A subdirectory within the project root that contains the core settings and configurations for your Django project.

It usually has the same name as your project.

Files in the Project Subdirectory:

  1. init.py

An empty file that tells Python to treat this directory as a package. It allows you to import other modules within the project.

  1. settings.py

Contains all the configuration settings for your Django project, such as database connections, installed apps, middleware, templates, static files, and more.

You can customize settings for different environments (development, staging, production) by splitting this file into multiple settings files (e.g., settings_dev.py, settings_prod.py).

  1. urls.py

The central URL configuration file. It maps URLs to their respective views. You can include other URL configurations here by importing them, making it easier to manage large projects.

  1. wsgi.py

Stands for "Web Server Gateway Interface." This file is used to deploy your project to a production web server. It acts as the entry point for the web server to serve your Django application.

  1. asgi.py

Stands for "Asynchronous Server Gateway Interface." It’s similar to wsgi.py, but it's used for handling asynchronous web requests (useful for WebSockets, HTTP2, etc.).

  1. Application Folders (my_app/)

Applications are reusable modules within a Django project. Each app typically handles a specific part of your project's functionality (e.g., users, blog, shop).

You can create new apps using:

python manage.py startapp my_app

Files in the Application Folder:

  1. init.py

Makes the directory a package, allowing Python to recognize it as a module.

  1. admin.py

Contains code to customize the Django Admin interface for this app. You can define how models are presented and managed in the admin panel.

  1. apps.py

Stores configuration for the app, like the app’s name and any custom behavior. It’s automatically created when the app is generated.

  1. models.py

Define the data models (tables) for your app. Django automatically creates tables based on these models when you run migrations.

  1. views.py

Contains the logic for handling web requests and returning responses. Views interact with models, templates, and other components.

  1. tests.py

Used to write unit tests for the app. It’s a good practice to write tests to ensure that your application works as expected.

  1. urls.py

Define URL patterns specific to the app. These can be included in the main urls.py to keep URL configurations modular.

  1. migrations/

Stores migration files that keep track of changes made to the models. Migrations are used to update the database schema automatically.

  1. static/

Store static files like CSS, JavaScript, and images related to the app. They are served separately from your application code.

  1. templates/

Contains HTML templates for the app. You can create subfolders to organize templates better (e.g., templates/my_app/index.html).

  1. Other Important Folders

  2. static/ (Global Static Folder)

Store static files (CSS, JavaScript, images) that are not tied to a specific app. During deployment, static files are collected from each app’s static/ directory and served to the client.

  1. templates/ (Global Templates Folder)

A central place to store templates that are used across the entire project. It’s beneficial for shared layouts and components.

  1. media/

Store user-uploaded files. Make sure to configure your settings.py to serve media files correctly.

Example Project Structure:

my_project/

├── manage.py
├── my_project/ # Project subdirectory
│ ├── init.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ └── asgi.py

├── my_app/ # An example application
│ ├── init.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── views.py
│ ├── urls.py
│ ├── tests.py
│ ├── migrations/
│ ├── static/
│ └── templates/

├── static/ # Global static files
│ └── css/
│ └── js/

└── templates/ # Global templates
└── base.html
└── index.html

This structure helps keep your project organized, scalable, and easy to maintain.

...

🔧 Announcing my new Django package: django-admin-export! #packaging #python #django


📈 30.18 Punkte
🔧 Programmierung

🔧 Django | Project Structure


📈 27.86 Punkte
🔧 Programmierung

🔧 Host a Django project documentation autogenerated with Sphinx on Read the Docs -- Django specifics


📈 25.29 Punkte
🔧 Programmierung

🕵️ Low CVE-2021-21416: Django-registration project Django-registration


📈 25.29 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2019-10682: Django-nopassword project Django-nopassword


📈 25.29 Punkte
🕵️ Sicherheitslücken

🔧 Structure is Structure


📈 25.25 Punkte
🔧 Programmierung

🔧 C# - Flatter structure vs Nested structure


📈 25.25 Punkte
🔧 Programmierung

📰 End-to-End Machine Learning Project (Part 1): Project Scoping, Structure, Data Gathering, EDA


📈 22.98 Punkte
🔧 AI Nachrichten

🔧 Django File Structure for Developers


📈 22.68 Punkte
🔧 Programmierung

🔧 RESTful API in Django [Django Rest Framework]


📈 20.12 Punkte
🔧 Programmierung

🔧 Django AllAuth Chapter 5 - Extending Django AllAuth user model with custom fields


📈 20.12 Punkte
🔧 Programmierung

🕵️ Django up to 1.11.18/2.0.10/2.1.5 django.utils.numberformat.format() denial of service


📈 20.12 Punkte
🕵️ Sicherheitslücken

🔧 Transform Your Django Images with django-imagekit


📈 20.12 Punkte
🔧 Programmierung

🕵️ Django up to 1.11.14/2.0.7 django.middleware.common.CommonMiddleware Open Redirect


📈 20.12 Punkte
🕵️ Sicherheitslücken

🔧 PostgreSQL für django aufsetzen - django in Produktion (Teil 2)


📈 20.12 Punkte
🔧 Programmierung

🔧 Django AllAuth Chapter 4 - Customizing Django AllAuth UI


📈 20.12 Punkte
🔧 Programmierung

🔧 Login with OTP Authentication in Django and Django REST Framework


📈 20.12 Punkte
🔧 Programmierung

🔧 A Beginner’s Guide to Django Web Framework: Django from Novice to Expert


📈 20.12 Punkte
🔧 Programmierung

🕵️ Django up to 1.8.17/1.9.12/1.10.6 django.utils.http.is_safe_url cross site scripting


📈 20.12 Punkte
🕵️ Sicherheitslücken

🔧 Django-Sonar: The Essential Debugging Tool for Django


📈 20.12 Punkte
🔧 Programmierung

🔧 Django AllAuth Chapter 3 - Social login with Django AllAuth


📈 20.12 Punkte
🔧 Programmierung

🕵️ Django up to 1.8.17/1.9.12/1.10.6 django.views.static.serve Open Redirect


📈 20.12 Punkte
🕵️ Sicherheitslücken

🔧 Crafting Modern Web APIs with Django and Django REST Framework: A Comprehensive Guide


📈 20.12 Punkte
🔧 Programmierung

🕵️ Django bis 1.11.14/2.0.7 django.middleware.common.CommonMiddleware Open Redirect


📈 20.12 Punkte
🕵️ Sicherheitslücken

🔧 FullStack Next.js & Django Authentication: Django REST, TypeScript, JWT, Wretch & Djoser


📈 20.12 Punkte
🔧 Programmierung

🔧 Authentication and Authorization in Django: Django session


📈 20.12 Punkte
🔧 Programmierung

🔧 Build a Multivendor E-commerce Website using Django, React & Django Rest Framework


📈 20.12 Punkte
🔧 Programmierung

🔧 Django AllAuth Chapter 2 - How to install and configure Django AllAuth


📈 20.12 Punkte
🔧 Programmierung

matomo