![]() |
This article will guide you in creating a Counter using React and Tailwind with the Django Framework. We’ll explore the integration of Django, React, and Tailwind, and go through the step-by-step process of implementing the Counter in Python using the Django Framework. What is Counter App?The Counter app is a straightforward tool designed for easy numeric tracking. Its minimalist interface prominently features a displayed number, accompanied by two intuitive buttons. Users can effortlessly increase the count by pressing the increment button or decrease it with the decrement button, providing a seamless and user-friendly experience for managing numerical values on the go. Counter using React and Tailwind using Django FrameworkHere, is the step-by-step implementation of Counter using React, Tailwind, and Django Framework. Here, we will cover the article in 2 parts, frontend and then backend. Backend Using DjangoTo start the project and app use this command django-admin startproject calculator_backend Now add this app to the ‘settings.py’ INSTALLED_APPS = [ To install the corsheaders run the below command in your terminal: pip install django-cors-headers
File Structure![]() File Strcuture Setting Necessary Filesmodels.py : below code defines a Django model named “Counter” with a single field “value” of type IntegerField, set with a default value of 50. This model can be used to represent a counter with an initial value 50. Python3
views.py :In below code Django REST Framework views define API endpoints for retrieving, incrementing, decrementing, and resetting a counter. The counter is represented by the “Counter” model, and the operations are performed based on HTTP POST requests, with corresponding responses containing messages and the updated counter value. Python3
serializers.py : In below code Django REST Framework serializer, “CounterSerializer,” is configured to serialize the “Counter” model, including only the ‘value’ field. It enables conversion between the Counter model instances and their JSON representations for use in the API views. Python3
counter_backend/urls.py : In below code Django URL configuration includes the default admin URLs and routes any URL starting with ‘api/’ to the URLs defined in the ‘counter.urls’ module, allowing the integration of the counter app’s API endpoints. Python3
counter/urls.py : In below code Django URL configuration for the ‘counter’ app maps specific paths to corresponding views: ‘get-counter/’ for retrieving the counter, ‘increment-counter/’ for incrementing it, ‘decrement-counter/’ for decrementing it, and ‘reset-counter/’ for resetting it Python3
Frontend Using React + TailwindTo start the project in react use this command npx create-react-app counter_frontend Install the necessary library tailwindcss using the below command npm install tailwindcss
File Structure![]() File Structure Creating User InterFaceApp.css: In below code this is a set of CSS styles for a React application or website. It includes styles for centering the content, setting the height and animation for a logo, defining the appearance of the header, and specifying colors and fonts. The keyframes section defines a spin animation for the logo, which rotates continuously from 0 to 360 degrees. CSS
App.js: In below code React component, ‘Counter.js’, uses the state hook to manage a counter value, fetches the initial value from a Django API endpoint using Axios in the ‘useEffect’ hook, and provides buttons to increment, decrement, and reset the counter through corresponding API calls. The component’s UI is styled with Tailwind CSS. Javascript
index.css: below code lines import Tailwind CSS base, components, and utilities styles, facilitating the use of predefined utility classes in your project’s stylesheets. CSS
index.js: This code sets up a React root using ReactDOM’s `createRoot` function, renders the `<App />` component inside a `<React.StrictMode>`, and includes a call to `reportWebVitals` for performance measurement. The application’s main entry point is ‘index.js’. Javascript
Deployement of the ProjectRun the server with the help of following command: python3 manage.py runserver Output ![]() Create a Counter using React and Tailwind using Django Framework |
Reffered: https://www.geeksforgeeks.org
Django |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |