VOOZH about

URL: https://www.geeksforgeeks.org/python/e-commerce-website-using-tailwind-and-react-using-django/

⇱ E-commerce Website using Tailwind and React using Django - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

E-commerce Website using Tailwind and React using Django

Last Updated : 28 Jun, 2024

Our e-commerce website, "ECOM," aims to offer users a smooth online shopping experience. We use Tailwind CSS for styling, React for interactive user interfaces, and Django for a strong backend. Shopify features a wide range of products across different categories, with functionalities like user authentication, product search, shopping cart management, and secure checkout. Here's how to create this e-commerce website:

Setup Backend (Django):

Step 1: Install Django and create a new project:

pip install django
👁 Backend---01
Backend - 01

Step 2: Create a Django Project

django-admin startproject ecommerce_backend
👁 Backend---02
Backend - 02

Step 3: Change your directory.

cd ecommerce_backend
👁 Backend-03
Backend - 03

Step 4: Use the below command to start your django app.

python manage.py startapp store
👁 Backend---04
Backend - 04

Step 5:Install and Set Up React

Navigate to the project directory and create a new React app:

npx create-react-app frontend
👁 frontend---01
FrontEnd - 01
cd frontend
👁 frontend-02
FrontEnd - 02

Step 6: Integrate Tailwind CSS with React

Install Tailwind CSS and its dependencies:

npm install -D tailwindcss postcss autoprefixer
👁 Frontend---03
Frontend - 03
npx tailwindcss init -p
👁 Frontend-04
Frontend - 04

Step 7: Now its time to add code to your website.

Configure Tailwind by editing tailwind.config.js:

Add the following to src/index.css:

Setup the Frontend with React and Tailwind

Creating Components

Step 1: Header Component

Step 2: Product Listing Component

This component will list all the product available to your database.

Add your product and category by adding code in your backend directory.

👁 add_category
add_category

add_product.html file will help you to add category to your database. Use this template to add category without entering to admin pannel.

👁 add_product
add_product

Now create 'static' folder to you root directory and create css folder and other folder to store your static files like css file, images, etc.

Fetching Data from Django Backend

Step 1: Set Up Django REST Framework

Install Django REST Framework:

pip install djangorestframework
👁 Fetching-Data-01
Fetching Data - 01

Step 2: Add it to INSTALLED_APPS in settings.py:

Step 3: Create API Endpoints

In store/views.py:

This file is available inside your products folder. Use products/urls.py to add your urls path.

In ecommerce_backend/urls.py. This file is the global file for your urls, please add the below code to seprate your app inside the project.

This is for your models.py file. Create it and add the below code.

Step 3: Fetch Data in React

Install Axios in React app:

npm install axios
👁 Fetching-Data-02
Fetching Data 02

Running the Project

Run Django Development Server

python manage.py runserver

Run React Development Server

npm start
👁 homepage
Home Page

Conclusion

By combining Tailwind CSS, React, and Django, you can build a modern, responsive, and scalable e-commerce website. Tailwind simplifies styling, React offers a robust front-end framework, and Django ensures a secure and efficient back end. This stack not only enhances development speed but also provides a maintainable codebase, making it an excellent choice for building e-commerce applications.

Comment
Article Tags: