Django follow unfollow
Hi,
I try to solve my problem. I have follow and unfollow button on profile page, and on this page button works but I need to create this button on other pages. My problem is to filter when user is in target or not.
On profile page I have code:
{% if profile_user.followed %} <a href="{% url 'unfollow' profile_user.id %}" class="btn btn-danger-gradiant btn-rounded letter-spacing-1" id="follow">Przestań obserwować</a> {% else %} <a href="{% url 'follow' profile_user.id %}" class="btn btn-danger-gradiant btn-rounded letter-spacing-1" id="follow">Obserwuj</a> {% endif %}
In views this looks like that:
def profile(request, username): ''' Shows the users profile ''' enricher = Enrich(request.user) profile_user = get_object_or_404(User, username=username) feed = feed_manager.get_user_feed(profile_user.id) activities = feed.get(limit=25)['results'] context = {} do_i_follow_users(request.user, [profile_user]) context['profile_user'] = profile_user context['activities'] = enricher.enrich_activities(activities) response = render(request, 'auth/user_detail.html', context) return response
This filter come from function "do_i_follow_users" as you can see in views. But how to take user in other situation like this:
def goalcomments(request, slug): goal = get_object_or_404(Goal, slug=slug) comments = goal.goalcomment.filter(active=True) if request.method == 'POST': form = CommentGoal(data=request.POST) if form.is_valid(): goal_comment = form.save(commit=False) goal_comment.author = request.user goal_comment.goal = goal goal_comment.save() else: form = CommentGoal() return render(request, 'goals/comments.html', {'goal': goal, 'comments': comments, 'form': form})
I don't know how to add this function "do_i_follow_users(request.user, [profile_user])" on view "commentgoal".
My Follow model is:
class Follow(Activity, models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='following_set') target = models.ForeignKey(User, on_delete=models.CASCADE, related_name='follower_set') created_at = models.DateTimeField(auto_now_add=True) deleted_at = models.DateTimeField(blank=True, null=True)
I must check if comment author is in target. Maybe someone help me.

Рекомендуем хостинг TIMEWEB
Стабильный хостинг, на котором располагается социальная сеть EVILEG. Для проектов на Django рекомендуем VDS хостинг.Вам это нравится? Поделитесь в социальных сетях!
Комментарии
Пожалуйста, авторизуйтесь или зарегистрируйтесь
- Unknown akadamn
- 24 января 2025 г. 17:14
Qt - Тест 001. Сигналы и слоты
- Результат:84баллов,
- Очки рейтинга4
- Unknown akadamn
- 24 января 2025 г. 16:22
Qt - Тест 001. Сигналы и слоты
- Результат:42баллов,
- Очки рейтинга-8


Hello,
Can you show source of do_i_follow_users function. I some misunderstand mechanism of this function. This function only check following relation, or create following relations ?
And I want suggest you to usi django-friendship battery. It is ready django-app for follow/unfollow relations, and for friendship relations.
Ok :)
This is this function:
I use django getstream.io to create feed etc. I send signal after user click follow or unfollow to getstream. My follow and unfollow views:
If you want check, is request.user in following users, then I think you can use custom template filter tag.
For example I have user_in filter
using
I think you can create something else like this
and use it
Thank you! For me work that:
and this templatetag: