[pinterest clone (33)] MultipleObjectMixin을 통한 Projectapp 구현
웹 프로그래밍

[pinterest clone (33)] MultipleObjectMixin을 통한 Projectapp 구현

728x90
반응형

articleapp의 model을 보면 writer만 있는데 어떤 project(게시판)의 글인지도 추가

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

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


class Article(models.Model):
    writer = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='arrticle', null=True)
    project = models.ForeignKey(Project, on_delete=models.SET_NULL, related_name='arrticle', null=True)
    title = models.CharField(max_length=200, null=True)
    image = models.ImageField(upload_to='article/', null=False)
    content = models.TextField(null=True)

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

 

글쓸때 어느 project에 넣을지 선택할 수 있게 forms.py도 수정

from django.forms import ModelForm

from articleapp.models import Article


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

 

model을 바꿨으니까 변경사항 db에 저장

makemigrations, migrate

 


게시판(project)에 새로 글을 넣어줬는데 article에서는 잘 나타나지만 게시판에는 아직 나타내주질 못했다.

View 안에서 mixin을 사용할것이다. 

 

 

projectapp/views.py - detailview를 보자.

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

오브젝트 하나만 가지고 있는데 추가를 해주겠다.

 

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)

MultipleObjectMixin: 여러가지 오브젝트를 다룰 수 있게 해주는 mixin 이다. 

 

 

detail.view 수정 (게시판 내 글 보이게 수정)

을 할건데 list.view를 snippets에 list_fragment.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>
            {% include 'snippets/list_fragment.html' with article_list=object_list%}
        </div>
    </div>

{% endblock %}

 

mypage 에도 자기가 쓴 글 보이게 만든걸 집어넣자.

 

accountapp/views.py

class AccountDetailView(DetailView, MultipleObjectMixin):
    model = User
    context_object_name = 'target_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)

detail.html

{% extends 'base.html' %}

{% block content %}

<div>
    <div style="text-align: center; max-width: 500px; margin: 4rem auto;">

        {% if target_user.profile %}
        <img src="{{ target_user.profile.image.url }}" alt="" style="height: 12rem; width: 12rem; border-radius: 20rem; margin-bottom: 2rem; object-fit: cover;">
        <h2 style="font-family: 'NanumSquareB'">
            {{ target_user.profile.nickname}}
                {% if target_user == user %}
                <a href="{% url 'profileapp:update' pk=target_user.profile.pk %}">
                    edit
                </a>
                {% endif %}
        </h2>
        <h5 style="margin-bottom: 3rem;">
            {{ target_user.profile.message }}
        </h5>
        {% else %}
            {% if target_user == user %}
            <a href="{% url 'profileapp:create' %}">
                <h2 style="font-family: 'NanumSquareB'">
                    Create Profile
                </h2>
            </a>
            {% else %}
            <h2>
                닉네임 미설정
            </h2>
            {% endif %}
        {% endif %}

        {% if target_user == user %}
        <a href="{% url 'accountapp:update' pk=user.pk %}">
            <p>
                Change Info
            </p>
        </a>
        <a href="{% url 'accountapp:delete' pk=user.pk %}">
            <p>
                Quit
            </p>
        </a>
        {% endif %}
    </div>

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

{% endblock %}

 

728x90
반응형