웹 프로그래밍

[pinterest clone (33)] RedirectView를 통한 subscribeapp

728x90
반응형

앞과정 생략 (과정에 대해 복습할땐 여기 참고)

duckracoon.tistory.com/69

 

[pinterest clone (32)] Projectapp 구현

1. Create / Detail / List View (Update / delete) 2. Success_url to related Project 3. Login_required to CreateView 4. Model : title / description / image / created_at 개발 순서는 MVT 순서로 습관을..

duckracoon.tistory.com

 

(M) models.py

from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from projectapp.models import Project


class Subscription(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='subscription')
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='subscription')

    class Meta:
        unique_together = ('user', 'project')

 Meta 보면 구독을 여러번 하지 못하게 하기 위한 설정이다. 유저와 프로젝트의 쌍은 유일하게 존재한다.

* 모델만들면 항상 db등록

 

(V) views.py

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404

# Create your views here.
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views.generic import RedirectView

from projectapp.models import Project
from subscribeapp.models import Subscription


@method_decorator(login_required, 'get')
class SubscriptionView(RedirectView):

    def get_redirect_url(self, *args, **kwargs):
        return reverse('projectapp:detail', kwargs={'pk': self.request.GET.get('project_pk')})

    def get(self, request, *args, **kwargs):
        project = get_object_or_404(Project, pk=self.request.GET.get('project_pk'))
        user = self.request.user

        subscription = Subscription.objects.filter(user=user, project=project)

        if subscription.exists():
            subscription.delete()
        else:
            Subscription(user=user, project=project).save()

        return super(SubscriptionView, self).get(request, *args, **kwargs)

(T)

projectapp/detail.html

{% extends 'base.html'%}

{% block content %}

    <div>
        <div style="text-align: center; max-width: 500px; margin: 4rem auto">
            <img src="{{ target_project.image.url }}" alt=""
                style="height: 12rem; width: 12rem; border-radius: 20rem; margin-bottom: 2rem; object-fit: cover;">
            <h2 style="font-family: 'NanumSquareB'">
                {{ target_project.title }}
            </h2>
            <h5 style="margin-bottom:3rem;">
                {{ target_project.description }}
            </h5>
        </div>

        <div class="text-center">
            {% if user.is_authenticated %}
            <a href="{% url 'subscribeapp:subscribe' %}?project_pk={{ target_project.pk }}">
                Subscribe
            </a>
            {% endif %}
        </div>
            
        <div>
            {% include 'snippets/list_fragment.html' with article_list=object_list%}
        </div>
    </div>

{% endblock %}

 

구독정보 유무 판별을 위한 projectapp/views.py 수정

-detailview

class ProjectDetailView(DetailView, MultipleObjectMixin):
    model = Project
    context_object_name = 'target_project'
    template_name = 'projectapp/detail.html'

    paginate_by = 25

    def get_context_data(self, **kwargs):
        project = self.object
        user = self.request.user

        if user.is_authenticated:
            subscription = Subscription.objects.filter(user=user, project=project)

        object_list = Article.objects.filter(project=self.get_object())
        return super(ProjectDetailView, self).get_context_data(object_list=object_list,
                                                               subscription=subscription,
                                                               **kwargs)

detail.html

{% extends 'base.html'%}

{% block content %}

    <div>
        <div style="text-align: center; max-width: 500px; margin: 4rem auto 1.5rem auto">
            <img src="{{ target_project.image.url }}" alt=""
                style="height: 12rem; width: 12rem; border-radius: 20rem; margin-bottom: 2rem; object-fit: cover;">
            <h2 style="font-family: 'NanumSquareB'">
                {{ target_project.title }}
            </h2>
            <h5 style="margin-bottom:3rem;">
                {{ target_project.description }}
            </h5>
        </div>

        <div class="text-center mb-5">
            {% if user.is_authenticated %}
            {% if not subscription %}
            <a href="{% url 'subscribeapp:subscribe' %}?project_pk={{ target_project.pk }}"
                class="btn btn-primary rounded-pill px-4">
                Subscribe
            </a>
            {% else %}
            <a href="{% url 'subscribeapp:subscribe' %}?project_pk={{ target_project.pk }}"
                class="btn btn-dark rounded-pill px-4">
                Unsubscribe
            </a>
            {% endif %}
            {% endif %}
        </div>

        <div>
            {% include 'snippets/list_fragment.html' with article_list=object_list%}
        </div>
    </div>

{% endblock %}

 

728x90
반응형