logo

JEONGGON

    블로그
github
mode
목 차
down-arrow

Django M:N 관계 pt.2

2023.04.04.

post-thumbnail

M:N 관계 (Follow 기능)

1. Profile

1-1. 프로필 페이지

- 프로필 url 설정

# accounts/urls.py

urlpatterns = [
    ...,
    path('profile/<username>', views.profile, name='profile'),
]

- 프로필 view 함수 작성

# accounts/views.py

from django.contrib.auth import get_user_model


def profile(request, username):
    User = get_user_model()
    person = User.objects.get(username=username)
    context = {
        'person': person,
    }
    return render(request, 'accounts/profile.html', context)

- 프로필 템플릿 작성

<!--accounts/profile.html-->

<h1>{{ person.username }}님의 프로필</h1>

<hr>

<h2>{{ person.username }}님의 게시글</h2>
{% for article in person.article_set.all %}
<div>{{ article.title }}</div>
{% endfor %}

<hr>

<h2>{{ person.username }}님의 댓글</h2>
{% for comment in person.comment_set.all %}
<div>{{ comment.content }}</div>
{% endfor %}

<hr>

<h2>{{ person.username }}님이 좋아요한 게시글</h2>
{% for article in person.like_articles.all %}
<div>{{ article.title }}</div>
{% endfor %}
<!--articles/index.html-->

<a href="{% url 'accounts:profile' user.username %}">내 프로필</a>

<p>작성자 : <a href="{% url 'accounts:profile' article.user.username %}">{{ article.user }}</a></p>


2. User & User

2-1. Many to many relationships

  • 유저는 0명 이상의 다른 유저와 관련됨
  • 유저는 다른 유저로부터 0개 이상의 팔로우를 받을 수 있고, 유저는 0명 이상의 다른 유저들에게 팔로잉을 할 수 있음

2-2. Follow 구현

- User 모델 수정

# accounts/models.py

class User(AbstractUser):
    followings = models.ManyToManyField('self', symmetrical=False, related_name='follewers')

- Follow 기능 url 설정

# accounts/urls.py

urlpatterns = [
    ...,
    path('<int:user_pk>/follow/', views.follow, name='follow'),
]

- Follow 기능 view 함수 작성

# accounts/views.py

@login_required
def follow(request, user_pk):
    User = get_user_model()
    person = User.objects.get(pk=user_pk)
    if person != request.user:
        if person.followers.filter(pk=request.user.pk).exists():
            person.followers.remove(request.user)
        else:
            person.followers.add(request.user)
    return redirect('accounts:profile', person.username)

- 프로필 유저의 팔로잉/팔로워 수 및 팔로우/언팔로우 버튼 출력

<!--accounts/profile.html-->

<div>
  <div>
    팔로잉 : {{ person.followings.all|length }} / 팔로워 : {{ person.followers.all|length }}
  </div>
  {% if request.user != person %}
  <div>
    <form action="{% url 'accounts:follow' person.pk %}" method="POST">
      {% csrf_token %}
      {% if request.user in person.followers.all %}
      <input type="submit" value="Unfollow">
      {% else %}
      <input type="submit" value="Follow">
      {% endif %}
    </form>
  </div>
  {% endif %}
</div>
djangoframeworkrelationalM:Nfollowusermany_to_many
profile

조정곤

주니어 프론트엔드 개발자

github
linkedin
instagram
email

< 이전글

Django Ajax를 이용한 비동기 요청

다음글 >

Django M:N 관계 pt.1

Django 포스트 (19)

down-arrow