you provided me code to create a instagram clone app but the like functionality is not working im providing you the code help me correct the code or provide a whole new insagram clone website with all it functionality like profile comment search user private user etc

feed.html

<!DOCTYPE html>
<html>
<head>
    <title>Feed</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <h2>Welcome to the Feed</h2>
    <p>Hello, {{ request.user.username }}!</p>
    <a href="{% url 'create_post' %}">Create New Post</a> |
    <a href="{% url 'profile' %}">Profile</a> |
    <a href="{% url 'logout' %}">Logout</a>
    <hr>
    {% for post in posts %}
        <div>
            <p><strong>{{ post.author.username }}</strong> - {{ post.created_at }}</p>
            <img src="{{ post.image.url }}" alt="Post Image" width="300">
            <p>{{ post.caption }}</p>
            <button class="like-button" data-post-id="{{ post.id }}">
                {% if user in post.likes.all.values_list('user', flat=True) %}
                    Unlike
                {% else %}
                    Like
                {% endif %}
            </button>
            <span id="like-count-{{ post.id }}">{{ post.likes.count }}</span> Likes
            <hr>
            <h4>Comments:</h4>
            {% for comment in post.comments.all %}
                <p><strong>{{ comment.author.username }}</strong>: {{ comment.text }}</p>
            {% empty %}
                <p>No comments yet.</p>
            {% endfor %}
            <form action="{% url 'add_comment' post.id %}" method="post">
                {% csrf_token %}
                {{ form.as_p }}
                <button type="submit">Add Comment</button>
            </form>
            <hr>
        </div>
    {% empty %}
        <p>No posts available.</p>
    {% endfor %}

    <div>
        <span class="step-links">
            {% if posts.has_previous %}
                <a href="?page=1">&laquo; first</a>
                <a href="?page={{ posts.previous_page_number }}">previous</a>
            {% endif %}

            <span class="current">
                Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
            </span>

            {% if posts.has_next %}
                <a href="?page={{ posts.next_page_number }}">next</a>
                <a href="?page={{ posts.paginator.num_pages }}">last &raquo;</a>
            {% endif %}
        </span>
    </div>

    <script>
        $(document).ready(function(){
            $('.like-button').click(function(){
                var btn = $(this);
                var postId = btn.data('post-id');
                $.ajax({
                    url: '/like/' + postId + '/',
                    type: 'GET',
                    success: function(response){
                        if(response.liked){
                            btn.text('Unlike');
                        } else {
                            btn.text('Like');
                        }
                        $('#like-count-' + postId).text(response.total_likes);
                    },
                    error: function(error){
                        console.log('Error:', error); 
                    }
                });
            });
        });
    </script>
</body>
</html>

models.py

from django.db import models
from django.contrib.auth.models import User
from PIL import Image
from django import forms
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone



class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    image = models.ImageField(upload_to='post_images')
    caption = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def _str_(self):
        return f'Post by {self.author.username} on {self.created_at.strftime("%Y-%m-%d")}'
    class Meta:
        ordering = ['-created_at']
        
    @property
    def liked_user_ids(self):
        return self.likes.values_list('user', flat=True)  # This will return a list of user IDs who liked the post
    
    likes = models.ManyToManyField(User, related_name='liked_posts', blank=True)
    
    @property
    def likes(self):
        return Like.objects.filter(post=self)
        


# class Like(models.Model):
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     post = models.ForeignKey(Post, on_delete=models.CASCADE)
#     created_at = models.DateTimeField(auto_now_add=True)

#     class Meta:
#         unique_together = ('user', 'post')  # Ensure a user can only like a post once
        
    
class Like(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)

    def _str_(self):
        return f'{self.user.username} likes {self.post.id}'    
    
        

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    text = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def _str_(self):
        return f'Comment by {self.author.username} on {self.post.id}'
    

class Follow(models.Model):
    follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='following')
    following = models.ForeignKey(User, on_delete=models.CASCADE, related_name='followers')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('follower', 'following')
    def _str_(self):
        return f'{self.follower.username} follows {self.following.username}'



class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(blank=True)
    profile_pic = models.ImageField(default='default.jpg', upload_to='profile_pics')



# class Like(models.Model):
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='likes')
#     created_at = models.DateTimeField(auto_now_add=True)
#     class Meta:
#         unique_together = ('user', 'post')


# class Comment(models.Model):
#     post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
#     author = models.ForeignKey(User, on_delete=models.CASCADE)
#     text = models.TextField()
#     created_at = models.DateTimeField(auto_now_add=True)


# class Follow(models.Model):
#     follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='following')
#     following = models.ForeignKey(User, on_delete=models.CASCADE, related_name='followers')
#     created_at = models.DateTimeField(auto_now_add=True)
#     class Meta:
#         unique_together = ('follower', 'following')


class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['username', 'email']


class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['bio', 'profile_pic']


class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['image', 'caption']


class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['text']
        widgets = {
            'text': forms.Textarea(attrs={'rows': 2, 'placeholder':
            'Add a comment...'}),
        }

views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.decorators import login_required
from .forms import UserUpdateForm, ProfileUpdateForm, PostForm, CommentForm
from .models import Profile, Post, Like, Comment, Follow
from django.core.paginator import Paginator
from django.http import JsonResponse
from django.db.models import Q
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from .models import User









def home(request):
    return render(request, 'home.html')  # Create a home.html template

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('feed')
    else:
        form = UserCreationForm()
    return render(request, 'users/register.html', {'form': form})



def user_login(request):
    if request.method == 'POST':
        form = AuthenticationForm(request, data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect('feed')
    else:
        form = AuthenticationForm()
    return render(request, 'users/login.html', {'form': form})

def user_logout(request):
    logout(request)
    return redirect('feed')


@login_required
def profile(request, user_id=None):
    if user_id:
        user = get_object_or_404(User, id=user_id)
    else:
        user = request.user

    is_following = Follow.objects.filter(follower=request.user, following=user).exists() if request.user != user else False
    posts = user.posts.all()

    return render(request, 'users/profile.html', {
        'profile_user': user,
        'is_following': is_following,
        'posts': posts
    })


@login_required
def edit_profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)

        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            return redirect('profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'users/edit_profile.html', context)



@login_required
def create_post(request):
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            new_post = form.save(commit=False)
            new_post.author = request.user
            new_post.save()
            return redirect('feed')

    else:
        form = PostForm()
    return render(request, 'users/create_post.html', {'form': form})



@login_required
def like_post(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    liked = False
    like, created = Like.objects.get_or_create(user=request.user, post=post)
    if not created:
        like.delete()
    else:
        liked = True
    return JsonResponse({'liked': liked, 'total_likes': post.likes.count()})




def add_comment(request, post_id):
    post = get_object_or_404(Post, id=post_id)  # Ensure post is fetched by ID
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.author = request.user
            comment.post = post
            comment.save()
            return redirect('feed')  # or wherever you want to redirect after saving the comment
    else:
        form = CommentForm()

    # Pass the post object to the template
    return render(request, 'users/add_comment.html', {'form': form, 'post': post})



def follow_user(request, user_id):
    target_user = get_object_or_404(User, id=user_id)
    if target_user != request.user:
        follow, created = Follow.objects.get_or_create(follower=request.user, following=target_user)
        if not created:
            follow.delete()
    return redirect('profile', user_id=target_user.id)


def profile(request, user_id=None):
    if user_id:
        user = get_object_or_404(User, id=user_id)
    else:
        user = request.user
        is_following = False
    if request.user != user:
        is_following = Follow.objects.filter(follower=request.user, following=user).exists()
    posts = user.posts.all()
    return render(request, 'users/profile.html', {'profile_user': user, 'is_following': is_following, 'posts': posts})



@login_required
def search(request):
    query = request.GET.get('q', '')
    users = User.objects.filter(username__icontains=query) if query else []
    posts = Post.objects.filter(Q(caption_icontains=query) | Q(authorusername_icontains=query)) if query else []
    return render(request, 'users/search.html', {'query': query, 'users': users, 'posts': posts})



def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('feed')
    else:
        form = UserCreationForm()
    return render(request, 'users/register.html', {'form': form})



def user_login(request):
    if request.method == 'POST':
        form = AuthenticationForm(request, data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect('feed')
    else:
        form = AuthenticationForm()
    return render(request, 'registration/login.html', {'form': form})



def profile(request, user_id=None):
    if user_id:
        user = get_object_or_404(User, id=user_id)
    else:
        user = request.user
        is_following = False
    if request.user != user:
        is_following = Follow.objects.filter(follower=request.user, following=user).exists()
    posts = user.posts.all()
    return render(request, 'users/profile.html', {'profile_user': user, 
                    'is_following': is_following, 'posts': posts})




def edit_profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)

        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            return redirect('profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)
        context = {
            'u_form': u_form,
            'p_form': p_form
        }
    return render(request, 'users/edit_profile.html', context)



def create_post(request):
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            new_post = form.save(commit=False)
            new_post.author = request.user
            new_post.save()
            return redirect('feed')
    else:
        form = PostForm()
    return render(request, 'users/create_post.html', {'form': form})    
    


@login_required
def feed(request):
    post_list = Post.objects.all()
    paginator = Paginator(post_list, 1) # Show 10 posts per page
    
    page_number = request.GET.get('page')
    posts = paginator.get_page(page_number)
    form = CommentForm()
    # for post in posts:
    #     post.likes_count = post.likes.count()
        # post.liked_by_users = post.likes.values_list('user', flat=True)  
    return render(request, 'users/feed.html', {'posts': posts, 'user': request.user})



def follow_user(request, user_id):
    target_user = get_object_or_404(User, id=user_id)
    if target_user != request.user: follow, created = Follow.objects.get_or_create(follower=request.user, following=target_user)
    if not created:
        follow.delete()
    return redirect('profile', user_id=target_user.id)


@login_required
def search(request):
    query = request.GET.get('q', '')
    users = User.objects.filter(username__icontains=query) if query else []
    posts = Post.objects.filter(Q(caption__icontains=query) |
    Q(author_username_icontains=query)) if query else []
    return render(request, 'users/search.html', {'query': query, 'users': users, 'posts': posts})

like functinality is not working