Skip to content

Commit

Permalink
User profile view
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEZE1020 committed Jan 12, 2025
1 parent 4e2694c commit 6945b72
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
18 changes: 18 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from django.contrib.auth import login, authenticate
from django.shortcuts import render, redirect
from django.shortcuts import render, get_object_or_404
from .models import CustomUser, Follow, Post

class UserViewSet(viewsets.ModelViewSet):
queryset = CustomUser.objects.all()
Expand Down Expand Up @@ -44,6 +46,22 @@ class UserUpdateView(UpdateView):
fields = ['username', 'email', 'bio', 'profile_picture']
success_url = '/'



def user_profile(request, user_id):
user = get_object_or_404(CustomUser, id=user_id)
posts = Post.objects.filter(user=user)
followers = Follow.objects.filter(following=user)
following = Follow.objects.filter(follower=user)
context = {
'user': user,
'posts': posts,
'followers': followers,
'following': following,
}
return render(request, 'accounts/profile.html', context)


@api_view(['POST'])
@permission_classes([IsAuthenticated])
def follow_user(request, user_id):
Expand Down
32 changes: 32 additions & 0 deletions templates/accounts/profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends 'base.html' %}

{% block content %}
<h1>{{ user.username }}</h1>
<img src="{{ user.profile_picture.url }}" alt="Profile Picture">
<p>{{ user.bio }}</p>

<h2>Followers</h2>
<ul>
{% for follower in user.followers.all %}
<li>{{ follower.follower.username }}</li>
{% endfor %}
</ul>

<h2>Following</h2>
<ul>
{% for following in user.following.all %}
<li>{{ following.following.username }}</li>
{% endfor %}
</ul>

{% if user != request.user %}
<form method="post" action="{% url 'follow-user' user.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-primary">Follow</button>
</form>
<form method="post" action="{% url 'unfollow-user' user.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-secondary">Unfollow</button>
</form>
{% endif %}
{% endblock %}
23 changes: 23 additions & 0 deletions templates/posts/feed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends 'base.html' %}

{% block content %}
<h1>Feed</h1>
<form method="post" action="{% url 'create-post' %}">
{% csrf_token %}
<textarea name="content" rows="3" class="form-control" placeholder="What's on your mind?"></textarea>
<button type="submit" class="btn btn-primary mt-2">Post</button>
</form>

{% for post in posts %}
<div class="card mt-4">
<div class="card-body">
<h5 class="card-title">{{ post.user.username }}</h5>
<p class="card-text">{{ post.content }}</p>
<form method="post" action="{% url 'like-post' post.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-light">Like ({{ post.likes.count }})</button>
</form>
</div>
</div>
{% endfor %}
{% endblock %}
1 change: 0 additions & 1 deletion templates/posts/post_list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- templates/posts/post_list.html -->
{% extends 'base.html' %}

{% block content %}
Expand Down

0 comments on commit 6945b72

Please sign in to comment.