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>
Django入门指南-第7章:模板引擎设置(完结)的更多相关文章
- Django入门指南-第9章:静态文件设置(完结)
http://127.0.0.1:8000 #下一步是告诉Django在哪里可以找到静态文件.打开settings.py,拉到文件的底部,在STATIC_URL后面添加以下内容: STATICFILE ...
- Django入门指南-第10章:Django Admin 介绍(完结)
在浏览器中打开该URL:http://127.0.0.1:8000/admin/ 我们可以检查一切是否正常,打开URL http://127.0.0.1:8000 我们首先创建一个管理员帐户: pyt ...
- Django入门指南-第8章:第一个测试用例(完结)
python manage.py test python manage.py test --verbosity=2 # boards/tests.py from django.core.urlreso ...
- Django入门指南-第6章:第一个视图函数(完结)
http://127.0.0.1:8000/ # boards/views.py from django.http import HttpResponse from .models import Bo ...
- 完整的Django入门指南学习笔记2
part2: 前沿 在第一节中,我们安装了项目所需要的一切:Python3.6以及在虚拟环境中运行的Django2.0,这部分教程继续在项目上编写代码. 开始写代码前,先讨论下项目的相关背景知识,然后 ...
- Django入门与实践 17-26章总结
Django入门与实践-第17章:保护视图 Django 有一个内置的视图装饰器 来避免它被未登录的用户访问: 现在如果用户没有登录,将被重定向到登录页面: 现在尝试登录,登录成功后,应用程序会跳转到 ...
- javaCV入门指南:序章
前言 从2016年6月开始写<javacv开发详解>系列,到而今的<javacv入门指南>,虽然仅隔了两年多时间,却也改变了很多东西. 比如我们的流媒体技术群从刚开始的两三个人 ...
- 完整的Django入门指南学习笔记7 网页自动翻译
转自[https://simpleisbetterthancomplex.com/series/2017/10/16/a-complete-beginners-guide-to-django-part ...
- 完整的Django入门指南学习笔记6
前言 欢迎来到系列教程的第六部分!在这篇教程中,我们将详细探讨基于类的视图(简称CBV).我们也将重构一些现有的视图,以便利用内置的基于类的通用视图(Generic Class-Based Views ...
随机推荐
- FireFox 书签 缓存 路径设置
English ver down https://www.mozilla.org/en-US/firefox/new/ add ons https://addons.mozilla.org/en-US ...
- jsp访问java变量
jsp页面中javascript访问 java的变量 <%= JAVA变量名%> jsp中嵌入java代码<%java代码%> --变量<% String JAVASOu ...
- mysql 自动执行事件
首先配置mysql的配置文件my.ini, 加上event_scheduler = 1 开启自动执行事件配置 demo drop event event_test; CREATE EVENT ev ...
- as3 代码优化
1 代码写法 1 定义局部变量 定义局部变量的时候,一定要用关键字var来定义,因为在Flash播放器中,局部变量的运行速度更快,而且在他们的作用域外是不耗占系统资源的.当一个函数调用结束的时候,相应 ...
- tensorflow UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
tensorflow读取图像出现错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid s ...
- Object-c 构造、析构函数
一.构造函数 在OC中凡是已init开头的函数我们都称之为构造函数,在声明构造函数的时候,不带参数的一般直接声明为“-(id)init”,带参数的一般声明为“-(id)initWith...”. @i ...
- Haskell语言学习笔记(55)Data.Vector
Data.Vector Construction Prelude V> import Data.Vector as V Prelude V> V.empty [] Prelude V> ...
- Haskell语言学习笔记(34)System.Environment
System.Environment getArgs :: IO [String] getProgName :: IO String getExecutablePath :: IO FilePath ...
- 第八章 高级搜索树 (xa4)红黑树:删除
- spring中配置监听队列的MQ
一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置. <bean id="axx" ...