Creating a Full Stack CRUD application with Django and Angular
1. Set Up the Backend (Django)
1.1 Install Django
Start by setting up a virtual environment and installing Django.
1.2 Start a Django Project
Create a new Django project and app.
1.3 Configure Django Project
Add api
, rest_framework
, and corsheaders
to your INSTALLED_APPS
in backend/settings.py
:
Add middleware for CORS
(to allow Angular to communicate with the backend):
1.4 Create the Database Model
In api/models.py
, define a simple model for a "Book" entity:
1.5 Create Migrations and Apply Them
Run the following commands to set up the database:
1.6 Create Serializers and Views
In api/serializers.py
, create a serializer for the Book
model:
In api/views.py
, add the views for CRUD operations:
1.7 Add URL Routing for the API
In api/urls.py
, define the URL patterns:
Include this in the main backend/urls.py
:
1.8 Run the Django Server
Start the Django server:
Test the endpoints at:
- http://127.0.0.1:8000/api/books/ (List and Create books)
- http://127.0.0.1:8000/api/books/<id>/ (Retrieve, Update, Delete a book)
2. Set Up the Frontend (Angular)
2.1 Install Angular CLI
Ensure Node.js and npm are installed. Install Angular CLI globally:
2.2 Create a New Angular Project
Generate a new Angular project:
2.3 Generate a Book Service and Component
Use Angular CLI to create a service and component for books:
2.4 Install Angular HTTP Client
Ensure HttpClientModule
is imported in app.module.ts
:
2.5 Implement Book Service
Edit book.service.ts
to interact with the Django backend:
2.6 Implement the Book Component
Edit book.component.ts
to perform CRUD operations:
2.7 Update the Book Component Template
Edit book.component.html
:
2.8 Run the Angular App
Start the Angular development server:
Visit http://localhost:4200 to interact with the frontend.
3. Testing and Final Touches
- Ensure the backend and frontend are running simultaneously.
- Test all CRUD operations.
- For production, use
ng build
to build the Angular app and serve it using Django or a static file server.
With this guide, you now have a fully functioning Django + Angular CRUD app.