Django学习视频6-10
学习的视频来自B站UP主“再敲一行代码” 链接地址。
该博客为自己学习过程中的一些总结记录,以便以后查阅,纯小白,有错误的地方欢迎多多指正哟。
视频6【略】
理论课讲解
视频7
7.1 导出依赖包
导出依赖包文档: pip freeze >requirements.txt
安装所依赖的包: pip install -r requirements.txt
视频8
8.1 button
<button type="button">
<a href="{% url 'blog_list' %}">博客首页</a>
</button>
8.2 a标签去掉下划线
<style>a{text-decoration:none}</style>
8.3 HTML date过滤器
<h4>最近更新时间:{{ blog.update_time|date:'Y-m-d H:i:s' }}</h4>
8.4 HTMLfor循环为空
{% for blog in blog_list %}
<hr>
<a href="{% url 'blog_detail' blog.id %}">
<h3>{{ blog.title }}</h3></a>
<p>分类:<a href="{% url 'blogs_with_type' blog.blog_type.pk%}">
{{ blog.blog_type }}</a></p>
<p>作者:{{ blog.author }}</p>
<p>最后发布于:{{ blog.update_time|date:'Y-m-d H:i:s' }}</p>
<p>{{ blog.content|truncatechars:30 }}</p>
{% empty %}
<p>--暂无博客,敬请期待!--</p>
{% endfor %}
8.5 HTML控制显示内容的字数
<p>{{ foo.content|truncatechars:30 }}</p>
8.6 通过外键查询同类博客
<p>分类:
<a href="{% url 'blogs_with_type' blog.blog_type.pk%}">
{{ blog.blog_type }}
</a>
</p>
def blogs_with_type(request,type_pk):
blog_type = get_object_or_404(BlogType, pk =type_pk)
content = {'blogs_with_type': Blog.objects.filter(blog_type=blog_type)}
return render(request, "blogs_with_type.html", content)
视频9
9.1 创建HTML模板
<!DOCTYPE html>
<html lang="en">
<style>a{text-decoration:none}</style>
<head>
<meta charset="UTF-8">
<title>
{% block title %}{% endblock %}
</title>
</head>
<body>
<div>
<a href="{% url 'blog_list' %}">博客首页</a>
</div>
{% block content%}{% endblock %}
</body>
</html>
9.2 使用HTML模板
{% extends "base.html" %}
{% block title %}
{{ blog.title }}
{% endblock %}
{% block content%}
<h2>{{ blog.title }}</h2>
<h4>作者:{{ blog.author }}</h4>
<h4>最近更新时间:{{ blog.update_time|date:'Y-m-d H:i:s' }}</h4>
<p>{{ blog.content }}</p>
{% endblock %}
9.3 模板搜索规则
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
#默认搜索路径变为BASE_DIR/templates/
#BASE_DIR为文件主路径
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
...
}
]
建立如下图的结构
----templates
---------base.html
---------blog
------------blog_detail.html
------------blog_list.html
views文件里面的路径也要改一下
return render(request, "blog/blog_list.html", content)
视频10
10.1 使用CSS模板
注意开头一定要有{% load staticfiles %}
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}
首页
{% endblock %}
{% block header %}
<link rel="stylesheet" href="{% static 'home.css' %}">
{% endblock %}
{% block content %}
<h1 class="home_display">欢迎来到祖安!</h1>
{% endblock %}