From 6945b722cc1494c91b924a9471ccd406e05c71a0 Mon Sep 17 00:00:00 2001 From: ZEZE1020 Date: Mon, 13 Jan 2025 02:15:46 +0300 Subject: [PATCH] User profile view --- accounts/views.py | 18 ++++++++++++++++++ templates/accounts/profile.html | 32 ++++++++++++++++++++++++++++++++ templates/posts/feed.html | 23 +++++++++++++++++++++++ templates/posts/post_list.html | 1 - 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 templates/accounts/profile.html create mode 100644 templates/posts/feed.html diff --git a/accounts/views.py b/accounts/views.py index c16fb34..2c36d76 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -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() @@ -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): diff --git a/templates/accounts/profile.html b/templates/accounts/profile.html new file mode 100644 index 0000000..c890063 --- /dev/null +++ b/templates/accounts/profile.html @@ -0,0 +1,32 @@ +{% extends 'base.html' %} + +{% block content %} +

{{ user.username }}

+Profile Picture +

{{ user.bio }}

+ +

Followers

+ + +

Following

+ + +{% if user != request.user %} +
+ {% csrf_token %} + +
+
+ {% csrf_token %} + +
+{% endif %} +{% endblock %} diff --git a/templates/posts/feed.html b/templates/posts/feed.html new file mode 100644 index 0000000..2a8b32e --- /dev/null +++ b/templates/posts/feed.html @@ -0,0 +1,23 @@ +{% extends 'base.html' %} + +{% block content %} +

Feed

+
+ {% csrf_token %} + + +
+ +{% for post in posts %} +
+
+
{{ post.user.username }}
+

{{ post.content }}

+
+ {% csrf_token %} + +
+
+
+{% endfor %} +{% endblock %} diff --git a/templates/posts/post_list.html b/templates/posts/post_list.html index 9ee998f..90329d8 100644 --- a/templates/posts/post_list.html +++ b/templates/posts/post_list.html @@ -1,4 +1,3 @@ - {% extends 'base.html' %} {% block content %}