UNC BACS 350

Web Apps with Python/Django

Logo

Lesson 21 - Bootstrap

LEARN

Office Hours

Django for Beginners - Chapter 10 - Bootstrap

Today

W3Schools

Adjusting form styles

Building a Theme

Using Bootstrap

Use both the Bootstrap and custom CSS

templates/unc.html

html <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="/static/style.css" rel="stylesheet">

Component Styling

CSS Color Classes

Set accent colors using a stylesheet

unc.css

```css .unc-dark { color: #F6B018; background-color: #0E3D65; }

.unc-light { color: #0E3D65; background-color: #F6B018; } ```

templates/unc.html

html <header class="unc-dark">UNC HEADER<header> <table class="unc-light"> ... <table>

CSS Color Classes

Bootstrap uses named color as classes

templates/unc.html

html <header class="bg-dark text-light">UNC HEADER<header> <div class="bg-light text-dark"> <button class="btn-primary">It worked</button> <p class="bg-dark text-light">This is a dark background with white text.</p> </div>

CSS Color Classes

Bootstrap uses named color as classes

/*    --primary:#007bff;      --secondary:#6c757d;*/
/*    --success:#28a745;      --info:#17a2b8;*/
/*    --warning:#ffc107;      --danger:#dc3545;*/
/*    --light:#f8f9fa;        --dark:#343a40;*/

Margin and Padding

Bootstrap uses classes in HTML to set spacing

templates/unc.html

```html

UNC HEADER

This is a text paragraph.

```

BUILD

HTML Files

. ├── lesson │ ├── 21.md │ ├── 22.md │ └── 23.md └── templates ├── components.html ├── grid.html ├── index.html ├── template.html └── typography.html

Lessons Django Project

Build a fresh demo project

$ django-admin startproject config .
$ python manage.py migrate
$ python manage.py runserver

Browse http://127.0.0.1:8000

config/urls.py

urlpatterns = [
    path('', TemplateView.as_view(template_name="index.html")),
]

Error no template for 'index.html'

config/settings.py

TEMPLATES = [
    {
        ...
        'DIRS': [BASE_DIR / 'templates'],
        ...
        },
    },
]

Error no template for 'typography.html'

![(img/404-template.png)]

Get the template name from URL

config/urls.py

class PageView(TemplateView):
    def get_template_names(self):
        return self.kwargs.get('page')


urlpatterns = [
    path('<str:page>', PageView.as_view()),
]

Error no template for 'index.html'