Django React Intergration
Django React Intergration
Prerequisites
Before getting started, You should have some basic knowledge
of ReactJS and Django Framework. You should also
have Node.js and Python installed on your machine.
Steps
Step 1: Create a New Django Project and a New React Application
The first step is to create a new Django project and a new React application.
You can create the Django project using the following command:
django-admin startproject myproject
This will create a new directory called myproject with the basic structure of a
Django project.
Next, you can create a new React application using the following command:
Once the project and application are created, we need to install some packages.
For Django, we need to install Django Rest Framework and Django Cors
Headers. For React, we need to install Axios for making HTTP requests.
To install Django Rest Framework and Django Cors Headers, run the
following command:
After installing the required packages, we can start building the API endpoints
in Django. We will create a new Django app to handle the API requests.
The API endpoint will return JSON data when we make HTTP requests to it.
We can use Django Rest Framework to build the API endpoints.
This will create a new directory called myapi with the basic structure of a Django
app.
Next, Open the settings.py file inside the myproject directory and add the following
lines of code:
INSTALLED_APPS = [
# ...
'rest_framework',
'corsheaders',
'myapi',
MIDDLEWARE = [
# ...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
These lines of code will add the rest_framework, corsheaders, and myapi apps to
the INSTALLED_APPS list, add the CorsMiddleware to the MIDDLEWARE list, and allow
all origins to make cross-origin requests.
@api_view(['GET'])
def hello_world(request):
return Response({'message': 'Hello, world!'})
This code defines a new API endpoint that returns a JSON response with the
message “Hello, world!”.
Add URLs :
Open the urls.py inside the myproject directory and add these lines:
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('myapi.urls'))
Finally, Create the urls.py file inside the myapi directory and add the following
lines of code:
from django.urls import path
urlpatterns = [
This code defines a new URL pattern that maps to the hello_world function
in views.py.
useEffect(() => {
axios.get('http://localhost:8000/hello-world/')
.then(response => {
setMessage(response.data.message);
})
.catch(error => {
console.log(error);
});
}, []);
return (
<div>
<h1>Hello, World!</h1>
<p>{message}</p>
</div>
);
This code defines a new React component called HelloWorld that makes an HTTP
GET request to the hello-world API endpoint we defined earlier. The response data
is stored in the message state, which is displayed on the web page.
Finally, we need to render the HelloWorld component inside the App.js file in
the src directory of the myapp directory. Replace the contents of App.js with the
following lines of code:
function App() {
return (
<div>
<HelloWorld />
</div>
);
This code renders the HelloWorld component inside the div element with
the root ID.
In the first window, navigate to the myproject directory and run the following
command:
python manage.py runserver
In the second window, navigate to the myapp directory and run the following
command:
npm start
Open your web browser and navigate to http://localhost:3000/. You should see the
message "Hello, world!" displayed on the web page.
Result,
Conclusion
Connecting ReactJS and Django Framework can be a powerful combination
for building web applications. By following the steps outlined in this tutorial,
you can connect these two tools and build a robust web application. Remember
to install the required packages and handle CORS errors to ensure smooth
communication between the React app and the Django API.
There are many tutorials and resources available online to help you get started
with connecting ReactJS and Django Framework. It’s important to carefully
follow the steps and ensure that all required packages are installed.
Additionally, be sure to handle CORS errors to avoid any issues with cross-
origin requests. With these considerations in mind, you can confidently connect
these two powerful tools and build a robust web application.