Expert IT Solutions

Discover New Ideas and Solutions with CodeEssence Blogs

Get inspired with our insightful blog posts covering innovative solutions, ideas, and strategies to elevate your business.

shape image
shape image
shape image
shape image
shape image
shape image
shape image
image

Building a Personal Blog Platform with Django and React

In this blog post, we will guide you through the steps to build a personal blog platform using Django for the backend and React for the frontend. This application will allow users to create, edit, and delete blog posts, as well as view all posts. Let's get started!

Prerequisites

  • Basic knowledge of Python and JavaScript
  • Django installed on your machine
  • Node.js and npm installed

Step 1: Setting Up Django Backend

First, let's create a new Django project and app.

Bash
django-admin startproject blog_platform
cd blog_platform
django-admin startapp blog

Next, add the new app to your settings.py file:

Python
INSTALLED_APPS = [
    ...,
    'blog',
]

Now, let's create a simple model for our blog posts in blog/models.py:

Python
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

After defining the model, run the following commands to create the database table:

Bash
python manage.py makemigrations
python manage.py migrate

Next, create a serializer for our model in blog/serializers.py:

Python
from rest_framework import serializers
from .models import Post

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = '__all__'

Now, we will create views to handle our API endpoints in blog/views.py:

Python
from rest_framework import viewsets
from .models import Post
from .serializers import PostSerializer

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer

Next, configure the URL routing in blog/urls.py:

Python
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import PostViewSet

router = DefaultRouter()
router.register(r'posts', PostViewSet)

urlpatterns = [
    path('', include(router.urls)),
]

Also, include these URLs in your main urls.py:

Python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('blog.urls')),
]

Don't forget to install Django REST Framework if you haven't already:

Bash
pip install djangorestframework

Now, run your Django server:

Bash
python manage.py runserver

Step 2: Setting Up React Frontend

Now, let's create a React application for our frontend.

Bash
npx create-react-app blog-frontend
cd blog-frontend

Install Axios for making API requests:

Bash
npm install axios

Now, let's create a simple component to fetch and display posts. Create a new file called Posts.js in the src directory:

Javascript
import React, { useEffect, useState } from 'react';
import axios from 'axios';

const Posts = () => {
    const [posts, setPosts] = useState([]);

    useEffect(() => {
        fetchPosts();
    }, []);

    const fetchPosts = async () => {
        const response = await axios.get('http://127.0.0.1:8000/api/posts/');
        setPosts(response.data);
    };

    return (
        

Blog Posts

    {posts.map(post => (
  • {post.title}
  • ))}
); }; export default Posts;

Next, import and use this component in src/App.js:

Javascript
import React from 'react';
import Posts from './Posts';

function App() {
    return (
        
); } export default App;

Now run your React application:

Bash
npm start

Conclusion

Congratulations! You have built a personal blog platform using Django and React. You can now create, view, and manage blog posts through your application. This setup provides a strong foundation for further enhancements, such as adding user authentication, comments, and more.

1 Comments:

  1. Nice post. I learn something new and challenging on websites I stumbleupon everyday. It will always be xciting to read through content from other writers and practice something from other sites.

Leave a Reply

Your email address will not be published. Required fields are marked *

Author *

Comment *