日期:2020/05/03
講者:Arashi
{% for post in post_list %}
<li>{{ post.text }}</li>
{% empty %}
<li>Sorry, no posts in this list.</li>
{% endfor %}
{% if post.text %}
text: {{ post.text }}
{% else %}
No text.
{% endif %}
{# test #}hello
{# {% if foo %}bar{% else %} #}
{% comment %}
<p>Commented out text with {{ create_date|date:"c" }}</p>
{% endcomment %}
單行註解
多行註解
{% load libraries name %}
{% extends "base.html" %}
base.html 可換成其他想要繼承過來的父模板名稱
定義一個可以被子模板覆蓋的區塊
<head>
{% block title %}
{% endblock %}
</head>
base.html
<head>
{% block title %}
<title>{{ post.title }} | My Adventure</title>
{% endblock %}
</head>
繼承 base 的 html
<!--新增-->
{% load static %}
<head>
<!--新增-->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
h1 {
color: #FCA205;
background-color: #A89797;
}
.blog/static/css/blog.css
body p {
font-style:italic;
}
將字體改為斜體
.blog/static/css/blog.css
body p {
border-style: solid;
border-width: 5px;
border-color: blue;
}
功用 | 語法 |
---|---|
虛線(點) | dotted |
虛線(條) | dashed |
實線 | solid |
雙邊框 | double |
.blog/static/css/blog.css
h2 a {
font-family: 'Dancing Script', cursive;
}
<head>
<!--增加-->
<link href="字體網址" rel="stylesheet">
</head>
.blog/templates/home.html
.blog/static/css/blog.css
.page-header {
background-color: #A89797;
margin-top: 0;
padding: 20px 20px 20px 40px;
}
<body>
<div class="page-header">
<h1><a href="/home">My Adventure</a></h1>
</div>
</body>
.blog/templates/home.html
.blog/static/css/blog.css
#test {
color: #FCA205;
padding-left: 50%;
}
<body>
<!--新增-->
<div id = "test">
<p>it just a test</p>
</div>
</body>
.blog/templates/home.html
.blog/static/css/blog.css
{% load static %}
<html>
<head>
<meta charset="utf-8">
{% block title %}
{% endblock %}
<style>
h1{
text-align: center;
}
.container{
display: flex;
justify-content: space-around;
}
</style>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
<link href="https://fonts.googleapis.com/css2?family=Dancing+Script:wght@500&display=swap" rel="stylesheet">
</head>
<body>
<div class="page-header">
<h1><a href="/home">My Blog</a></h1>
</div>
{% block content %}
{% endblock %}
</body>
</html>
{% extends 'base.html' %}
<head>
{% block na %}
<title>My Blog</title>
{% endblock %}
</head>
<body>
{% block content %}
<div class="container">
{% for post in post_list %}
<div class="post">
<h2><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h2>
<p>published: {{ post.published_date }}</p>
<p>{{ post.text|linebreaksbr|slice:":30" }}</p>
</div>
{% endfor %}
</div>
<div id = "test">
<p>it just a test</p>
</div>
{% endblock %}
</body>
HP:1/100