작정하고 Django (41) - MultipleObjectMixin을 통한 Projectapp 마무리

김의찬·2023년 6월 28일

작정하고 Django !

목록 보기
42/45

MultipleObjectMixin 사용

article 을 작성하고 이를 project에 연결하는 작업을 수행해야 하는데 이 때 MultipleObjectMixin 사용.

일단 articleapp 에서의 models.py 와 forms.py를 수정해야한다.
(project와의 연결을 위함)

class Article(models.Model):
    writer = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='article', null=True)

    project = models.ForeignKey(Project, on_delete=models.SET_NULL, related_name='article', null=True)


    title = models.CharField(max_length=200, null=True)
    image = models.ImageField(upload_to='article/', null=True)
    content = models.TextField(null=True)

    created_at = models.DateField(auto_now_add=True,null=True)

models.py에서는 project 변수를 추가한다.

class ArticleCreationForm(ModelForm):
    class Meta:
        model = Article
        fields = ['title', 'image', 'project', 'content']

forms.py에 project 필드를 하나 추가한다.

이를 수행하고 난 뒤 model을 변경했기 때문에 makemigrations과 migrate작업을 수행하여 DB변동작업을 거쳐야한다.

Projectapp views.py 수정

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):
        object_list = Article.objects.filter(project=self.get_object())
        return super(ProjectDetailView,self).get_context_data(object_list=object_list, **kwargs)

article과의 연결을 위해서 projectDetailView를 수정한다.

매개변수에 MultipleObjectMixin을 사용하고

get_context_data 함수를 작성한다.

snippets에 list_fragment.html 을 작성한다.


{% load static %}


<style>


    .container {
        padding: 0;
        margin: 0, auto;

    }
    .container a {
        width: 45%;
        max-width: 250px;
    }

    .container div {
      display: flex;
      justify-content: center;
      align-items: center;

      border-radius: 1rem;
    }

    .container img {
      width: 100%
      border-radius: 1rem;
    }

</style>
    {% if article_list %}
    <div class="container">
        {% for article in article_list %}
        <a href="{% url 'articleapp:detail' pk=article.pk %}">
            {% include 'snippets/card.html' with article=article %}
        </a>
        {% endfor %}
    </div>
    <script src="{% static 'js/magicgrid.js' %}"></script>
    {% else %}
    <div class="text-center">
        <h1>No Articles YET!</h1>
    </div>
    {% endif %}

    {% include 'snippets/pagination.html' with page_obj=page_obj %}

    <div style="text-align: center">
        <a href="{% url 'articleapp:create' %}" class="btn btn-dark rounded-pill col-3 mt-3">
            Create Article
        </a>
    </div>


detail.html에는

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

다음과 같은 코드를 추가한다.

article과 project를 연결했고 우리는 account 계정에서 mypage를 들어갔을 때 내가 작성한 article들을 보이게 하고 싶다.

마찬가지로 article과 account를 연결해주어야한다.

class AccountDetailView(DetailView, MultipleObjectMixin):
    model = User
    context_object_name = 'target_user'  # user 모델의 별명 느낌.
    template_name = 'accountapp/detail.html'

    paginate_by = 25

    def get_context_data(self, **kwargs):
        object_list = Article.objects.filter(writer=self.get_object())
        return super(AccountDetailView,self).get_context_data(object_list=object_list, **kwargs)

다음과 같이 AccountDetailView도 수정한다.

또한 detail.html에
다음과 같은 코드를 추가한다.

    <div>

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

    </div>
profile
김의찬입니다

0개의 댓글