UNC BACS 350

Web Apps with Python/Django

Logo

Card View Design Pattern

Overview

Goals

Build a common way to display content on web pages organized as cards.

Provide one place in code for each feature.

Encapsulate all common logic for reuse.

Build views without thinking (up and running in 5 minutes).

Use Bootstrap to create high visual impact.

Use Partial Template "cards.html" to organize your code logic.

Pass in data as a dictionary.

Steps

Data

Single card

python def card_data(title, body, color='', width='', link=None): html = markdown(body) return dict(title=title, body=html, color=color, width=width)

Multiple Cards

python def cards_data(): return [ card_data("Card One", text1), card_data("Card Two", text2), card_data("Card Three", text3), ]

Template

Document Template

templates/cards.html

```html {% for card in cards %}

<div class="card">
    <div class="card-header">
        <h2>{{ card.title }}</h2>
    </div>
    <div class="card-body">

        {% autoescape off %}
        <p>{{ card.body }}</p>
        {% endautoescape %}

    </div>
</div>

{% endfor %} ```

Views

workshop/views.py

```python class CardView(TemplateView): template_name = 'card.html'

def get_context_data(self, **kwargs):
    return dict(cards=cards_data())

```

URL Routes

workshop/urls.py

python urlpatterns = [ path('card', CardView.as_view(), name='card'), ]

Tests

workshop/tests.py

```python from django.test import TestCase

class ViewTest(TestCase):

def test_card_view(self):
    response = self.client.get('card/')
    self.assertContains(response, 'Card One')

```