(Django 기초) ListView

Posted by : at

Category : Django



# articleapp\views.py

# ...

class ArticleListView(ListView):
    model = Article
    context_object_name = 'article_list'
    template_name = 'articleapp/list.html'
    paginate_by = 25
# articleapp\urls.py

# ...

urlpatterns = [
    path('list/', ArticleListView.as_view(), name='list'),

    path('create/', ArticleCreateView.as_view(), name='create'),
    path('detail/<int:pk>', ArticleDetailView.as_view(), name='detail'),
    path('update/<int:pk>', ArticleUpdateView.as_view(), name='update'),
    path('delete/<int:pk>', ArticleDeleteView.as_view(), name='delete'),
]
<!-- header.html -->
<div class="pragmatic_header">
    <div>
        <h1 class="pragmatic_logo">Pragmatic</h1>
    </div>
    <div>
        <a href="{% url 'articleapp:list' %}">
            <span>Articles</span>
        </a>
        <span>nav</span>
        {% if not user.is_authenticated %}
        <a href="{% url 'accountapp:login' %}?next={{ request.path }}">
            <span>login</span>
        </a>
        <a href="{% url 'accountapp:create' %}">
            <span>SignUp</span>
        </a>
        {% else %}
        <a href="{% url 'accountapp:logout' %}?next{{ request.path }}">
            <span>logout</span>
        </a>
        <a href="{% url 'accountapp:detail' pk=user.pk %}">
            <span> MyPAge</span>
        </a>
        {% endif %}
    </div>
</div>
<!-- articleapp\list.html -->
{% extends 'base.html' %} 
{% load static %} 
{% block content %} 
  <style> 
    .container div { 
      width: 250px; 
      background-color: antiquewhite; 
      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 %} 
    <div style="text-align:center;"> 
        <a href="{% url 'articleapp:create' %}" class="btn btn-dark rounded-pill col-3 mt-3 mb-3"> 
            Create Article 
        </a> 
    </div> 
{% endblock %}
<!-- articleapp\card.html -->
<div>
  <img src="{{ article.image.url }}" alt="">
</div>


About Taehyung Kim

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

Star
Useful Links