2024年4月1日 星期一

在pythonanywhere設計Django網站的卡片(Card)功能(新增For Template)

延續文章: 在pythonanywhere設計Django網站的卡片(Card)功能


1.修改mysite/templates/vegetable.html的程式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html>
<head>
  {% load bootstrap5 %}
  {% bootstrap_css %}
  {% bootstrap_javascript %}
  {% load static %}
</head>
<body>
<div class="container">
  <ul class="nav bg-info">
    <li class="nav-item">
      <a class="nav-link link-light" href="/">首頁</a>
    </li>
    <li class="nav-item">
      <a class="nav-link link-light" href="/vegetable/">蔬菜</a>
    </li>
    <li class="nav-item">
      <a class="nav-link link-light" href="/fruit/">水果</a>
    </li>
  </ul>

<h1>蔬菜</h1>
<div class="row">
    {% for vegetable in vegetables %}
        <div class="card" style="width:300px">
            <img class="card-img-top" src="{% static "images/" %}{{ vegetable.image }}" alt="Card image">
            <div class="card-img-overlay">
                <h4 class="card-title" style="color:white;">{{ vegetable.title }}</h4>
                <p class="card-text" style="color:white;">{{ vegetable.subtitle }}</p>
                <a href="#" class="btn btn-primary">說明</a>
            </div>
        </div>
    {% endfor %}
</div>
  {% block content %}
  {% endblock %}
</div>
</body>
</html>

2.修改mysite/myapp/views.py的程式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from django.shortcuts import render

# Create your views here.
def index(request):
    slide1 = {'image':'fruits01.jpg', 'active':'active', 'interval':'10000', 'title':'水果01', 'subtitle':'好水果在雲林'}
    slide2 = {'image':'fruits02.jpg', 'active':'', 'interval':'2000', 'title':'水果02', 'subtitle':'好水果在雲林'}
    slide3 = {'image':'fruits03.jpg', 'active':'', 'interval':'3000', 'title':'水果03', 'subtitle':'好水果在雲林'}
    slides = [slide1, slide2, slide3]
    return render(request, 'index.html', locals())

def vegetable(request):
    vegetable1 = {'image':'vegetable01.jpg','title':'蔬菜01', 'subtitle':'好蔬菜在雲林'}
    vegetable2 = {'image':'vegetable02.jpg','title':'蔬菜02', 'subtitle':'好蔬菜在雲林'}
    vegetable3 = {'image':'vegetable03.jpg','title':'蔬菜03', 'subtitle':'好蔬菜在雲林'}
    vegetable4 = {'image':'vegetable04.jpg','title':'蔬菜04', 'subtitle':'好蔬菜在雲林'}
    vegetables = [vegetable1, vegetable2, vegetable3, vegetable4]
    return render(request, 'vegetable.html', locals())

def fruit(request):
    return render(request, 'fruit.html', locals())


沒有留言:

張貼留言