First Django Project
This chapter will guide you through creating your first Django project, understanding the basic structure of a Django project, and running your first web application.
Creating a Django Project
Using django-admin to Create a Project
Project Structure Analysis
manage.py Detailed Explanation
Main uses of manage.py:
- Run development server
- Create applications
- Execute database migrations
- Create superuser
- Collect static files
settings.py Detailed Explanation
urls.py Detailed Explanation
Running Development Server
Starting the Server
Accessing the Website
Open your browser and visit http://127.0.0.1:8000/, you should see the Django welcome page:
Custom Server Configuration
Database Initialization
Applying Initial Migrations
Creating a Superuser
Accessing Admin Interface
Visit http://127.0.0.1:8000/admin/ and log in using the superuser account you just created.
Creating Your First App
Django Project vs App
Project: Container for the entire website App: Python package that implements specific functionality
Creating an App
App Structure Analysis
App Files Detailed Explanation
apps.py - App Configuration
models.py - Data Models
views.py - View Functions
admin.py - Admin Interface
Writing Your First View
Creating View Functions
Configuring URL Routing
1. Create App URL Configuration
2. Include App URLs in Project URL Configuration
Registering the App
Testing Views
URL Configuration Details
URL Pattern Syntax
Path Converters
Custom Path Converters
URL Naming and Reverse Resolution
Project Configuration Optimization
Environment Variable Configuration
Separate Configuration Files
Using Different Configurations
Common Management Commands
Project Management Commands
Database Management Commands
User Management Commands
Other Useful Commands
Project Deployment Preparation
Production Environment Checklist
requirements.txt
Static Files Configuration
Chapter Summary
This chapter covered creating your first Django project in detail:
Key Points:
- Project Creation: Use django-admin startproject to create a project
- Project Structure: Understand Django's file organization
- App Creation: Use manage.py startapp to create apps
- URL Configuration: Set up URL routing and view mapping
- Development Server: Run and test Django applications
Important Concepts:
- Project vs App: Project is the container, app is the functional module
- MVT Architecture: Model-View-Template design pattern
- URL Routing: Map URLs to view functions
- Configuration Management: Use settings.py to manage project configuration
Best Practices:
- Create separate apps for each feature
- Use meaningful URL patterns and names
- Separate configuration files for different environments
- Use environment variables to manage sensitive information
- Follow Django project structure conventions
Development Workflow:
- Create project and app
- Configure URL routing
- Write view functions
- Test functionality
- Optimize configuration
In the next chapter, we will dive deeper into Django's basic concepts, including settings, middleware, app configuration, and other core knowledge.