Django Cheat-Sheet: From Scratch to Advanced Features.
1058 words • 6 min read
Introduction
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. This cheat-sheet will guide you through creating a Django project from scratch, adding applications, creating static and dynamic views, setting up a database, utilizing Django's ORM for complex queries, and more. By the end of this guide, you will have a solid understanding of Django's core concepts and how to use them effectively.
Setting Up Django
Installing Django
Ensure you have Python installed, then install Django using pip:
Creating a New Django Project
To create a new Django project, run:
Navigate into your project directory:
Running the Development Server
Start the development server with:
You can access the development server at http://127.0.0.1:8000/.
Creating and Adding an App
Creating a New App
Create a new app within your project:
Registering the App
Add your new app to the INSTALLED_APPS list in myproject/settings.py:
Creating Static Views
Basic View
Create a simple view in myapp/views.py:
Mapping the URL
Map the URL to your view in myapp/urls.py:
Ensure myapp/urls.py is included in the project’s urls.py:
Creating Dynamic Views
Template Rendering
Create a template in myapp/templates/myapp/home.html:
Update the view to use a template:
Adding a Database
Configuring the Database
By default, Django uses SQLite, configured in myproject/settings.py:
Creating Models with Various Field Types
In myapp/models.py, define your models using different field types:
Making Migrations
Create and apply migrations:
Django ORM and Data Fetching
Adding Data
You can add data using the Django admin or shell:
Fetching Data
In myapp/views.py, fetch and display data:
Create a template myapp/templates/myapp/items.html:
Complex Queries and QuerySets
Filtering and Querying
You can perform various queries using Django's ORM:
Serializers and JSON Response
Django REST Framework
Install Django REST Framework:
Add it to INSTALLED_APPS:
Creating a Serializer
Create a serializer in myapp/serializers.py:
Creating an API View
In myapp/views.py, create an API view:
Mapping the URL
In myapp/urls.py, add the API endpoint:
HTML Response
You can return HTML responses using templates, which allows you to dynamically generate HTML pages based on data.
Base Template
Create a base template myapp/templates/base.html:
Extending the Base Template
Create a new template myapp/templates/myapp/new_page.html:
Additional Concepts
Middleware
Middleware processes requests globally before reaching the view. Create custom middleware in myapp/middleware.py:
Add it to MIDDLEWARE in settings.py:
Forms and Validation
Create a form in myapp/forms.py:
Handle the form in your view:
Create a template myapp/templates/myapp/new_item.html:
User Authentication
Use Django's built-in authentication views for user login and logout:
Conclusion
This comprehensive cheat-sheet provides a detailed guide to getting started with Django and covers essential features such as creating dynamic views, handling forms, working with databases, performing complex queries, and creating APIs using Django REST Framework. With this knowledge, you can build robust and scalable web applications using Django.