This is a simple application which uses React and Django frameworks.
This project is a simple web application that demonstrates the integration of a React.js frontend with a Django backend. The frontend consists of a simple web page that fetches and displays data from the backend’s API endpoints.
/api/endpoint1/
: Returns a JSON response with a custom message saying Hola./api/endpoint2/
: Returns a JSON response with a custom message saying Adios.myproject/
├── myproject/ # Django project folder
│ ├── settings.py # Django settings
│ ├── urls.py # Project URL configuration
│ └── ...
├── myapp/ # Django app folder
│ ├── views.py # API views
│ ├── urls.py # App URL configuration
│ └── ...
└── manage.py # Django management script
myfrontend/ # React project folder
├── src/
│ ├── components/
│ │ └── MyComponent.js # React component that makes API calls
│ ├── App.js # Main React app file
│ └── ...
└── package.json # Node.js dependencies and scripts
Make sure Python
is installed in your machine.
Clone the repository: ```bash git clone https://github.com/yourusername/react-django-simple-website.git cd react-django-simple-website/myproject
Create a virtual environment and install dependencies present in requirements.txt
:
```bash
python -m venv env_site
source env_site/bin/activate # On Windows: .\env_site\Scripts\activate.ps1
pip install -r requirements.txt
For starting a new project: ```bash django-admin startproject projectReactDjango cd projectReactDjango python manage.py runserver
Make the changes to the code and then run the following commands.
Configuring CORS:
1. Add 'corsheaders' to INSTALLED_APPS in myproject/settings.py
INSTALLED_APPS = [
...
'corsheaders',
...
]
2. Add CorsMiddleware to MIDDLEWARE in myproject/settings.py:
MIDDLEWARE = [
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
3. Configure CORS settings in myproject/settings.py
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000",
# Add any other allowed origins here
]
Apply migrations: ```bash python manage.py makemigrations python manage.py migrate
Start the Django development server: ```bash python manage.py runserver
Make sure Node.js
is installed in your machine.
Make sure that node.js is properly installed or not in command prompt: ```bash node -v
For creating react app globally and check whether properly installed: ```bash npm install -g create-react-app create-react-app –version
Navigate to the frontend directory for creating app: ```bash npx create-react-app myfrontend cd ../myfrontend
Install dependencies present in package.json
:
```bash
npm install
Make the changes to the code and then run the following commands.
Ensure that the Django backend server is running: ```bash cd myproject python manage.py runserver
The backend server should be accessible at http://localhost:8000
.
Open a new terminal and start the React frontend server: ```bash cd myfrontend npm start
The frontend server should be accessible at http://localhost:3000
.
This project is licensed under the MIT License. See the LICENSE file for details.