Django React LeadFlow project
LeadFlow (or LeadFlo, if you prefer a shorter name) is a fantastic, simple project for a beginner portfolio because it clearly demonstrates the ability to handle forms, API submission, and data persistence—all critical full-stack skills.
LeadFlow project using React and Django:
📝 LeadFlow Project Breakdown
Goal:
To build a simple system where users can submit contact information (leads) via a React form, and have that data securely saved and viewed via a Django REST API.
1. ⚙️ Backend Setup (Django & DRF)
Model (leads/models.py)
You will need a simple model to represent the submitted data.
# Pseudo-code for models.py
class Lead(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(unique=True)
message = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
Serializer (leads/serializers.py)
This converts the Python object into JSON for the API.
# Pseudo-code for serializers.py
class LeadSerializer(serializers.ModelSerializer):
class Meta:
model = Lead
fields = ('id', 'name', 'email', 'message', 'created_at')
Views (leads/views.py)
Use a generic viewset to handle all API operations easily.
# Pseudo-code for views.py
class LeadViewSet(viewsets.ModelViewSet):
queryset = Lead.objects.all().order_by('-created_at')
serializer_class = LeadSerializer
# Optional: If you add authentication later, you'd add permission classes here.
API Endpoint
* POST to /api/leads/: Creates a new Lead (used by the React form).
* GET from /api/leads/: Retrieves all the Leads (for a simple admin viewer).
2. ⚛️ Frontend Setup (React)
Component 1: LeadForm
This component handles user input and API submission.
* State: Holds the values for name, email, and message.
* Form: A simple HTML form with corresponding inputs.
* Action: On submit, use Axios or fetch to send a POST request to your Django endpoint (/api/leads/).
* Validation: Basic client-side validation (e.g., ensuring the email field is not empty).
Component 2: LeadsList (Optional but Recommended)
A simple component to view the submitted data.
* Action: On component mount, use Axios or fetch to send a GET request to the endpoint.
* Display: Render the list of leads in a simple table or list format.
Key Integration Point: CORS
Remember to configure Cross-Origin Resource Sharing (CORS) in your Django settings so that your React app (running on one port, like 3000) can communicate with your Django backend (running on another port, like 8000). You will need the django-cors-headers package.
LeadFlow project bootstrapped with the necessary commands for both the Django API and the React frontend.
🚀 Project Setup Commands
1. Django Backend (API) Setup
We'll use venv for isolated environments and pip for package management.
| Step | Command | Description |
|---|---|---|
| A. Create Project Folder | mkdir LeadFlow | Creates the main project directory. |
| B. Navigate & Init Env | cd LeadFlow and then python -m venv venv | Enters the directory and creates a virtual environment. |
| C. Activate Environment | Linux/macOS: source venv/bin/activate Windows: .\venv\Scripts\activate | Activates the virtual environment. |
| D. Install Dependencies | pip install django djangorestframework django-cors-headers | Installs Django, DRF (for the API), and CORS (to connect React). |
| E. Create Django Project | django-admin startproject leadflow_backend . | Creates the core Django project files (note the .). |
| F. Create Django App | python manage.py startapp leads | Creates the specific application that will handle your Lead model and API. |
| G. Apply Migrations | python manage.py migrate | Sets up the initial database tables (Users, Sessions, etc.). |
2. React Frontend Setup
Assuming you have Node.js and npm/yarn/npx installed, we'll use Vite for a fast setup.
| Step | Command | Description |
|---|---|---|
| A. Create React App | npm create vite@latest leadflow_frontend -- --template react | Creates a new React project using Vite in a separate directory. |
| B. Install Dependencies | cd leadflow_frontend and then npm install axios | Navigates into the React directory and installs Axios (the preferred tool for making API calls). |
| C. Start React Development | npm run dev | Starts the React frontend development server (usually on http://localhost:5173). |
3. Key Configuration Notes
Before you start coding the features, you must complete these two essential steps:
A. Backend Configuration (Django)
In your Django settings file (leadflow_backend/settings.py):
* Register Apps: Add your new apps to the INSTALLED_APPS list.
INSTALLED_APPS = [
# ... existing apps
'rest_framework',
'corsheaders',
'leads',
]
* Enable CORS: Add corsheaders.middleware.CorsMiddleware to the top of your MIDDLEWARE list.
* Allow React Origin: Define where your React app will be running (the origin that is allowed to talk to your API).
CORS_ALLOWED_ORIGINS = [
"http://localhost:5173", # Update with your actual React dev server port
]
B. Frontend Connection (React)
In your React code, every time you make an API call, you will target the Django server address.
* API Base URL (e.g., in axios.js or App.jsx): http://localhost:8000/api/leads/
Once you've run these commands, you'll have two separate, connected development environments ready to start implementing the LeadFlow model, serializer, and form components!
Comments
Post a Comment