已发布的文章列表展示页面,展示文章标题;
blog/models.py中创建数据模型后,Django会自动提供数据库抽象的API(ORM),进行增删改查操作;
使用命令python manage.py shell
进入交互模式,进行数据库操作练习:
$ python manage.py shell
Python 3.7.2 (default, Dec 29 2018, 00:00:04)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: <
继续编写代码:
In [1]: from django.contrib.auth.models import User
In [2]: from blog.models import BlogArticles
In [3]: user = User.objects.get(username="baby_hua")
In [4]: user.username
Out[4]: 'baby_hua'
In [5]: user.id
Out[5]: 1
In [6]: user.password
Out[6]: 'pbkdf2_sha256$180000$f1G0ToF5qCJ1$yc+vGbCio5qIF3BsZTbYZw2OC5+Dv9tbTL0XxvKkCKw='
In [7]: user.email
Out[7]: '[email protected]'
In [8]: type(user)
Out[8]: django.contrib.auth.models.User
In [9]: blogs = BlogArticles.objects.all()
In [10]: blogs
Out[10]: <QuerySet [<BlogArticles: 这是一篇简单的博客5>, <BlogArticles: 这是一篇简单的博客4>, <BlogArticles: 这是一篇简单的博客3>, <BlogArticles: 这是一篇简单的博客2>, <BlogArticles: 这是一篇简单的博客1>]>
In [11]: for blog in blogs:
...: print(blog.title)
...:
这是一篇简单的博客5
这是一篇简单的博客4
这是一篇简单的博客3
这是一篇简单的博客2
这是一篇简单的博客1
In [12]: exit()
如读取文章标题,可以写一个函数blog_title()
进行实现,该函数通常被写在blog/views.py
文件中;views.py是视图文件,在其中实现的类似blog_title()
的函数,我们称之为“视图函数”,视图就变成“基于函数的视图
”(后续还有“基于类的视图
”的编写方式);
from django.shortcuts import render
# Create your views here.
from .models import BlogArticles
def blog_title(request):
blogs = BlogArticles.objects.all()
return render(request, "blog/titles.html", {"blogs": blogs})
blog_title()
函数:
render()
函数:可以将数据渲染到指定模板上:
render_to_response()
效果相同,区别在于render()
作为快捷方式,会自动使用RequestContext
;blog/titles.html
是标题列表在前端展示的页面,一般被称为模板;templates
templates
目录是Django默认存放当前应用所需模板的目录;Django会在运行时自动查找render()函数中指定的模板;blog
目录下新建子目录templates
,结构规范如下;templates
base.html
blog
titles.html
templates/base.html
基础模板代码示例:
{% block name %}
表示这是一个名为name的块标签
,用来包裹自定义内容;结束标签为{% endblock %}
;
<html lang="zh-cn">
<head>
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{% endblock %}title>
<link rel="stylesheet" href="http://necolas.github.io/normalize.css/">
<link rel="stylesheet" href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css">
head>
<body>
<div class="container">
{% block content %}
{% endblock %}
div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js">script>
<script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js">script>
body>
html>
templates/blog/titles.html
标题列表模板代码示例:
{% extends "base.html" %}
{% block title %}
blog titles
{% endblock %}
{% block content %}
<div class="row text-center vertical-middle-sm">
<h1>我的博客h1>
div>
<div class="row">
<div class="col-xs-12 col-md-8">
<ul>
{% for blog in blogs %}
<li>{{ blog.title }}li>
{% endfor %}
ul>
div>
<div class="col-xs-6 col-md-4">
<h2>广告h2>
<p>读书笔记p>
<img width="200px"
src="https://i0.hdslb.com/bfs/face/a0d8a470a7b7b1175c9cc43a88ee4adcd9a97851.jpg@68w_68h.webp">
div>
div>
{% endblock %}
函数和模板编写好,再配置URL,就可以通过网页访问了:
http://127.0.0.1:8000/blog/
的地址时,通过URL配置,将此请求转向blog
应用的urls.py
:mysite1/urls.py
中配置应用的URL(这个文件顶部注释介绍了如何配置应用的URL);urls.py
中配置该应用具体的URL;(没有urls.py文件则创建下)mysite1/urls.py
中配置:
"""mysite1 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("blog/", include("blog.urls")),
]
blog/urls.py
中配置:
from django.conf.urls import url
from . import views
urlpatterns = [
# r"^$" :表示访问当前应用的根目录,对应:http://127.0.0.1:8000/blog/
# views.blog_title :声明了响应这个请求的函数对象
url(r"^$", views.blog_title, name="blog_title"),
]