larken

勤奋的人生才有价值

导航

Django入门指南-第7章:模板引擎设置(完结)

<!--templates/home.html-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Boards</title>
    </head>

    <body>
        <h1>Boards</h1>
        {% for board in boards %}
            {{ board.name }} <br>
        {% endfor %}
    </body>
</html>   
TEMPLATES = [
    {
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
    },
]

#我们可以使用Python shell进行调试:
  python manage.py shell
  from django.conf import settings
  settings.BASE_DIR
  import os
  os.path.join(settings.BASE_DIR, 'templates')    
# boards/views.py
from django.shortcuts import render
from .models import Board

def home(request):
    boards = Board.objects.all()
    return render(request, 'home.html', {'boards': boards})


<!--我们可以用一个更漂亮的表格来替换,改进HTML模板:-->
<!--templates/home.html-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Boards</title>
    </head>

    <body>
        <h1>Boards</h1>
        <table border="1">
            <thead>
                <tr>
                    <th>Board</th>
                    <th>Posts</th>
                    <th>Topics</th>
                    <th>Last Post</th>
                </tr>
            </thead>
            <tbody>
                {% for board in boards %}
                    <tr>
                        <td>{{ board.name }}<br>
                <
small style="color: #888">{{ board.description }}</small>
              </
td> <td>0</td> <td>0</td> <td></td> </tr> {% endfor %} </tbody> </table> </body> </html>

 

posted on 2018-09-05 09:38  larken  阅读(269)  评论(0编辑  收藏  举报