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 %}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值