- 什么是Django
- 浏览器上网的基本原理
- 环境配置
Python3.7安装
https://www.python.org/ftp/python/3.7.8/python-3.7.8-amd64.exe
Django安装
python37 -m pip install Django
Conda安装
https://blog.csdn.net/ITLearnHall/article/details/81708148
PyCharm IDE安装与激活
https://docs.qq.com/doc/DR3Z3aXlxYUZMRFFp
- Django命令
启动一个项目:startproject
启动一个应用:startapp
检查项目完整:check
本地运行项目:runserver
执行用例测试:test
创建迁移文件:makemigrations
执行迁移文件:migrate
进入Python Shell环境:shell
把数据库数据导出到文件:dumpdata
把文件数据导入到数据库:loaddata
- 初识项目
django-admin startproject django_introduction
settings.py:项目配置文件
urls.py:路由配置文件
wsgi.py:wsgi应用的文件内容
python37 manage.py runserver
- 初识应用
Django应用 vs Django项目
python37 manage.py startapp blog
应用目录
- Django视图 & Django路由
设置解释器为python3.7
views.py
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("hello world")
blog.urls.py
from django.urls import path, include
import blog.views
urlpatterns = [
path('hello_world', blog.views.hello_world)
]
django_introduction.urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls'))
]
settings.py
INSTALLED_APPS = [
...
'blog.apps.BlogConfig'
]
- 模型层
- 创建博客文章模型
models.py
from django.db import models
class Article(models.Model):
# 文章ID
article_id = models.AutoField(primary_key=True)
# 文章标题
title = models.TextField()
# 文章摘要
brief_content = models.TextField()
# 文章内容
content = models.TextField()
# 文章日期
publish_date = models.DateTimeField(auto_now=True)
python37 manage.py makemigrations
python37 manage.py migrate
- 初识Django Shell
python37 manage.py shell
>>> from blog.models import Article
>>> a =Article()
>>> a.title = 'Django Test Shell'
>>> a.digest = 'Giao'
>>> a.content = 'Django Test Main content'
>>> a.save()
>>> print(a)
Article object (1)
>>> print(a.title)
Django Test Shell
>>> all = Article.objects.all()
>>> x = all[0]
>>> print(a.content)
Django Test Main content
- 初识Django Admin模块
创建管理员
python37 manage.py createsuperuser
Username (leave blank to use 'yydl'): Du1in9
Email address:
Password:
Password (again):
Superuser created successfully.
admin.py
from django.contrib import admin
from blog.models import Article
admin.site.register(Article)
python37 manage.py runserver
访问登录:http://127.0.0.1:8000/admin/
为了看到标题,models.py新增语句
def __str__(self):
return self.title
新建文章,Ctrl+C关闭,重启
- 实现博客数据返回页面
views.py
from blog.models import Article
...
def article_content(request):
article = Article.objects.all()[0]
title = article.title
brief_content = article.brief_content
content = article.content
article_id = article.article_id
publish_date = article.publish_date
return_str = 'title: %s, brief_content: %s, content: %s, article_id: %s, publish_date: %s' % (title, brief_content, content, article_id, publish_date)
return HttpResponse(return_str)
blog.urls.py
path('content', blog.views.article_content)
python37 manage.py runserver
访问:http://127.0.0.1:8000/blog/content
- 使用Bootstrap实现静态博客页面
Bootstrap官网:https://v3.bootcss.com/
templates.index.html
一给我里GiaoGiao
BootCDN简介 —— by Du1in9
文章标题1
Vue 是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。
Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。
jQuery 是一个高效、精简并且功能丰富的 JavaScript 工具库。它提供的 API 易于使用且兼容众多浏览器,
这让诸如 HTML 文档遍历和操作、事件处理、动画和 Ajax 操作更加简单。
文章标题2
ECharts 是一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,
兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等),底层依赖轻量级的矢量图形库 ZRender,
提供直观,交互丰富,可高度个性化定制的数据可视化图表。
Backbone 为复杂 Javascript 应用程序提供模型(models)、集合(collections)、视图(views)的结构。
其中模型用于绑定键值数据和自定义事件;集合附有可枚举函数的丰富 API;
视图可以声明事件处理函数,并通过 RESTful JSON 接口连接到应用程序。
templates.detail.html
Django Web框架入门
文章标题1
在有些场合,需要对Django处理的每个request都执行某段代码。 这类代码可能是在view处理之前修改传入的request,或者记录日志信息以便于调试,等等。2
这类功能可以用Django的中间件框架来实现,该框架由切入到Django的request/response处理过程中的钩子集合组成。
每个中间件组件都用于某个特定的功能。 如果你是顺着这本书读下来的话,你应该已经多次见到“中间件”了
第12章中所有的session和user工具都籍由一小簇中间件实现(例如,由中间件设定view中可见的 request.session 和 request.user )。
第13章讨论的站点范围cache实际上也是由一个中间件实现,一旦该中间件发现与view相应的response已在缓存中,就不再调用对应的view函数。
第14章所介绍的 flatpages , redirects , 和 csrf
等应用也都是通过中间件组件来完成其魔法般的功能。这个轻量级低层次的plug-in系统,能用于全面的修改Django的输入和输出。1
每个中间件组件都用于某个特定的功能。 如果你是顺着这本书读下来的话,你应该已经多次见到“中间件”了
第12章中所有的session和user工具都籍由一小簇中间件实现(例如,由中间件设定view中可见的 request.session 和 request.user )。
第13章讨论的站点范围cache实际上也是由一个中间件实现,一旦该中间件发现与view相应的response已在缓存中,就不再调用对应的view函数。
第14章所介绍的 flatpages , redirects , 和 csrf 等应用也都是通过中间件组件来完成其魔法般的功能。
- 模板系统基本语法
变量标签:{{ x }}
for循环标签:{% for x in list %},{% endfor %}
if-else分支标签:{% if %},{% else %},{% endif %}
templates.template.html
Ordering notice
Ordering notice
Dear {{ person_name }},
Thanks for placing an order from {{ company }}. It's scheduled to
ship on {{ ship_date|date:"F j, Y" }}.
Here are the items you've ordered:
{% for item in item_list %}
- {{ item }}
{% endfor %}
{% if ordered_warranty %}
Your warranty information will be included in the packaging.
{% else %}
You didn't order a warranty, so you're on your own when
the products inevitably stop working.
{% endif %}
Sincerely,
{{ company }}
- 模板系统渲染页面
使用以下脚本导入data.article的多篇文章
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_introduction.settings')
django.setup()
from blog.models import Article
data_path = '../data/article'
def main():
content_list = []
files = os.listdir(data_path)
for name in files:
f = os.path.join(data_path, name)
with open(f, 'r', encoding='utf-8') as f:
content = f.read()
item = (name[:-4], content[:100], content)
content_list.append(item)
# Article.objects.all().delete()
for item in content_list:
print('saving article: %s' % item[0])
article = Article()
article.title = item[0]
article.brief_content = item[1]
article.content = item[2]
article.save()
if __name__ == '__main__':
main()
templates.index.html
{% for article in article_list %}
{{ article.title }}
{{article.digest}}
{% endfor %}
最新文章
{% for article in article_list %}
{{ article.title }}
{% endfor %}
templates.detail.html
{{ curr_article.title }}
{% for section in section_list %}
{{ section }}
{% endfor %}
views.py
from django.shortcuts import render
...
def get_index_page(request):
all_article = Article.objects.all()
return render(request, 'blog/index.html',
{
'article_list':all_article
}
)
def get_detail_page(request):
curr_article = Article.objects.all()[1]
section_list = curr_article.content.split('\n')
return render(request, 'blog/detail.html',
{
'curr_article': curr_article,
'section_list': section_list
}
)
blog.urls.py
path('index', blog.views.get_index_page),
path('detail', blog.views.get_detail_page)
- 实现页面的跳转
1)指定id跳转文章详情页
urls.py
# path('detail', blog.views.get_detail_page),
path('detail/', blog.views.get_detail_page)
views.py
def get_detail_page(request, id):
all_article = Article.objects.all()
curr_article = None
for article in all_article:
if article.article_id == article_id:
curr_article = article
break
2)主页跳转文章详情页
index.html
{{ article.title }}
...
{{ article.title }}
- 实现上下篇文章跳转
detail.html
views.py
def get_detail_page(request, article_id):
all_article = Article.objects.all()
curr_article = None
previous_article = None
next_article = None
previous_index = 0
next_index = 0
for index, article in enumerate(all_article):
# 前面
if index == 0:
previous_index = 0
next_index = index + 1
# 后面
elif index == len(all_article) - 1:
previous_index = index - 1
next_index = index
# 中间
else:
previous_index = index - 1
next_index = index + 1
# article
if article.article_id == article_id:
curr_article = article
previous_article = all_article[previous_index]
next_article = all_article[next_index]
break
...
'previous_article': previous_article,
'next_article': next_article
- 实现分页功能
1)主页按钮
index.html