(Django 기초) ProjectApp - MultipleObjectMixin

Posted by : at

Category : Django



# articleapp\models.py

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)
# articleapp\forms.py

class ArticleCreationForm(ModelForm):
    class Meta:
        model = Article
        fields = ['title', 'image','project' ,'content']
$ python manage.py makemigrations
$ python manage.py migrate
# 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: 'NnumSquareB'">
      {{ target_project.title }}
    </h2>
    <h5 style="margin-bottom: 3rem;">
      {{ target_project.description }}
    </h5>
  </div>

  <div>

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

  </div>

</div>

{% endblock %}
<!-- projectapp\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 'articleapp/snippets/card.html' with article=article %}
        </a>
      {% endfor %}
      </div>
      <script src="{% static 'js/magic-grid.js' %}"></script>
    {% else %}
      <div style="text-align:center;">
          <h1>No Articles YET!</h1>
      </div>
    {% endif %}

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

    <div style="text-align:center;">
        <a href="{% url 'articleapp:create' %}" class="btn btn-dark rounded-pill mt-3 mb-3 px-3">
            Create Article
        </a>
    </div>
# 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)

# ...
<!-- accountapp\detail.html -->
{% extends 'base.html' %}
{% load bootstrap4 %}

{% block content %}

  <div>
    <div style="text-align: center; max-width: 500px; margin: 4rem auto;">
      <p>
        {{ target_user.date_joined }}
      </p>


      {% if target_user.profile %}

      <img src="{{ target_user.profile.image.url }}" alt="" style="height: 12rem; width: 12rem; border-radius: 20rem; margin-bottom: 3rem;">

      <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: 4rem">
        {{ 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>

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

  </div>

{% endblock %}

About Taehyung Kim

안녕하세요? 8년차 현업 C++ 개발자 김태형이라고 합니다. 😁 C/C++을 사랑하며 다양한 사람과의 협업을 즐깁니다. ☕ 꾸준한 자기개발을 미덕이라 생각하며 노력중이며, 제가 얻은 지식을 홈페이지에 정리 중입니다. 좀 더 상세한 제 이력서 혹은 Private 프로젝트 접근 권한을 원하신다면 메일주세요. 😎

Star
Useful Links