Win+R
键,输入cmd
回车,打开命令提示符窗口pip install django==2.0
$ django-admin help
Type 'django-admin help ' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
Creat New Project
:django-admin startproject django_introduction
python manage.py runserver
,访问提示的网址,提示django项目创建完成即可python manage.py startapp blog
from django.http import HttpResponse
# Create your views here.
def hello_world(request):
return HttpResponse("Hello World")
import blog.views
urlpatterns = [
path('hello_world', blog.views.hello_world)
]
from django.contrib import admin
from django.urls import path, include
from blog import views
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls'))
]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# myapp(添加此行进行应用声明)
'blog.apps.BlogConfig'
]
from django.db import models
# Create your models here.
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.DateField(auto_now=True)
python manage.py makemigrations
创建模型迁移文件python manage.py migrate
执行迁移文件python manage.py shell
$ from blog.models import Article
# 新建文章
$ a = Article()
$ a.title = 'Django shell Test'
$ a.brief_content = 'Django shell Test brief content'
$ a.content = 'test django shell new article content.'
$ print(a) # 打印预览
$ a.save() # 保存文章
# 读取文章
$ article = Article.objects.all()[0]
$ print(article.title,article.brief_content,article.content)
python manage.py addsuperuser
按提示设定用户名及密码即可from django.contrib import admin
# Register your models here.
from .models import Article
admin.site.register(Article)
# 在Django 的admin模块中返回title
def __str__(self):
return self.title
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,breif_content:%s,content:%s,article_id:%s,publish_date:%s' % (title, brief_content, content, article_id, publish_date)
return HttpResponse(return_str)
from django.urls import path, include
import blog.views
urlpatterns = [
path('hello_world', blog.views.hello_world),
path('content', blog.views.article_content) #将视图函数路由到 /blog/content 下
]
在blog文件夹下新建templates/blog文件夹用于存放html模板文件
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从零开始3小时Django开发个人博客系统title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous">script>
head>
<body>
<div class="container page-header">
<h1>从零开始3小时Django开发个人博客系统
<small>——来SCsmall>
h1>
div>
<div class="container page-body">
<div class="col-md-9" role="main">
<div class="body-main">
<div>
<h2><a href="#">文章标题1a>h2>
<p>
文章缩略信息
p>
div>
<div>
<h2><a href="#">文章标题2a>h2>
<p>
文章缩略信息
p>
div>
<div>
<h2><a href="#">文章标题3a>h2>
<p>
文章缩略信息
p>
div>
div>
div>
<div class="col-md-3" role="complementary">
<div>
<h2>最新文章h2>
<h4><a href="#">文章标题4a>h4>
<h4><a href="#">文章标题3a>h4>
<h4><a href="#">文章标题2a>h4>
<h4><a href="#">文章标题1a>h4>
div>
div>
div>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从零开始3小时Django开发个人博客系统title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous">script>
head>
<body>
<div class="container page-header">
<h1>文章标题h1>
div>
<div class="container body-main">
<div>
<p>文章内容p>
div>
div>
body>
html>
变量标签:{{ 变量 }}
示例{{ now }}
for循环标签:{% for x in list %},{% endfor %}
示例
{% for item in list %}
- {{ item }}
{% endfor %}
if-else标签:{% if %},{% else %},{% endif %}
示例:
{% if true %}
it's true
{% else %}
it's false
{% endif %}
def get_index_page(request):
all_article = Article.objects.all()
return render(request, 'blog/index.html', # 定义模板文件
{'article_list':all_article} # 返回整个文章列表)
pass
from django.urls import path, include
import blog.views
urlpatterns = [
path('hello_world', blog.views.hello_world),
path('content', blog.views.article_content),
path('index', blog.views.get_index_page)
]
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从零开始3小时Django开发个人博客系统title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous">script>
head>
<body>
<div class="container page-header">
<h1>从零开始3小时Django开发个人博客系统
<small>——来SCsmall>
h1>
div>
<div class="container page-body">
<div class="col-md-9" role="main">
<div class="body-main">
{% for article in article_list %}
<div>
<h2><a href="#">{{ article.title }}a>h2>
<p>
{{ article.brief_content }}
p>
div>
{% endfor %}
div>
div>
<div class="col-md-3" role="complementary">
<div>
<h2>最新文章h2>
{% for article in top5_article_list %}
<h4><a href="#">{{ article.title }}a>h4>
{% endfor %}
div>
div>
div>
body>
html>
def get_detail_page(request):
curr_article = Article.objects.all()[0]
section_list = curr_article.content.split('\n')
return render(request, 'blog/detail.html', {'curr_article': curr_article, 'section_list': section_list})
pass
from django.urls import path, include
import blog.views
urlpatterns = [
path('hello_world', blog.views.hello_world),
path('content', blog.views.article_content),
path('index', blog.views.get_index_page),
path('detail', blog.views.get_detail_page)
]
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从零开始3小时Django开发个人博客系统title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous">script>
head>
<body>
<div class="container page-header">
<h1>{{ curr_article.title }}h1>
div>
<div class="container body-main">
<div>
{% for section in section_list %}
<p>{{ section }}p>
{% endfor %}
div>
div>
body>
html>
from django.urls import path, include
import blog.views
urlpatterns = [
path('hello_world', blog.views.hello_world),
path('content', blog.views.article_content),
path('index', blog.views.get_index_page),
path('detail/' , blog.views.get_detail_page)
]
def get_detail_page(request, article_id):
all_article = Article.objects.all()
curr_article = None
for article in all_article:
if article.article_id == article_id:
curr_article = article
break
section_list = curr_article.content.split('\n')
return render(request, 'blog/detail.html', {'curr_article': curr_article, 'section_list': section_list})
pass
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从零开始3小时Django开发个人博客系统title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous">script>
head>
<body>
<div class="container page-header">
<h1>从零开始3小时Django开发个人博客系统
<small>——来SCsmall>
h1>
div>
<div class="container page-body">
<div class="col-md-9" role="main">
<div class="body-main">
{% for article in article_list %}
<div>
<h2><a href="/blog/detail/{{ article.article_id }}">{{ article.title }}a>h2>
<p>
{{ article.brief_content }}
p>
div>
{% endfor %}
div>
div>
<div class="col-md-3" role="complementary">
<div>
<h2>最新文章h2>
{% for article in top5_article_list %}
<h4><a href="/blog/detail/{{ article.article_id }}">{{ article.title }}a>h4>
{% endfor %}
div>
div>
div>
body>
html>
<nav aria-label="...">
<ul class="pager">
<li><a href="#">Previousa>li>
<li><a href="#">Nexta>li>
ul>
nav>
def get_detail_page(request, article_id):
all_article = Article.objects.all()
curr_article = None
previous_article = None
next_article = None
for index, article in enumerate(all_article):
if index == 0:
previous_article_index = 0
next_article_index = index + 1
elif index == len(all_article) - 1:
previous_article_index = index - 1
next_article_index = index
else:
previous_article_index = index - 1
next_article_index = index + 1
if article.article_id == article_id:
curr_article = article
previous_article = all_article[previous_article_index]
next_article = all_article[next_article_index]
break
section_list = curr_article.content.split('\n')
return render(request, 'blog/detail.html', {'curr_article': curr_article, 'section_list': section_list,'previous_article': previous_article,'next_article': next_article})
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从零开始3小时Django开发个人博客系统title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous">script>
head>
<body>
<div class="container page-header">
<h1>{{ curr_article.title }}h1>
div>
<div class="container body-main">
<div>
{% for section in section_list %}
<p>{{ section }}p>
{% endfor %}
div>
div>
<div>
<nav aria-label="...">
<ul class="pager">
<li><a href="/blog/detail/{{ previous_article.article_id }}">上一篇:{{ previous_article.title }}a>li>
<li><a href="/blog/detail/{{ next_article.article_id }}">下一篇:{{ next_article.title }}a>li>
ul>
nav>
div>
body>
html>
from django.core.paginator import Paginator
导入即可paginator = Paginator(文章列表,每页文章数)
paginator.num_pages # 获取总分页数
paginator_article_list = paginator.page(page) # 获取每页文章列表
paginator_article_list.has_next(1) # 第一页是否有下一页,返回值为布尔类型
paginator_article_list.has_previous(2) # 第二页是否有上一页,返回值为布尔类型
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«span>
a>
li>
<li><a href="#">1a>li>
<li><a href="#">2a>li>
<li><a href="#">3a>li>
<li><a href="#">4a>li>
<li><a href="#">5a>li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»span>
a>
li>
ul>
nav>
def get_index_page(request):
page = request.GET.get("page")
if page:
page = int(page)
else:
page = 1
print("page_num:", page)
all_article = Article.objects.all()
top5_article_list = Article.objects.order_by('-publish_date')[:10]
paginator = Paginator(all_article, 6)
page_num = paginator.num_pages
print('page_num:', page_num)
page_article_list = paginator.page(page)
if page_article_list.has_next():
next_page = page + 1
else:
next_page = page
if page_article_list.has_previous():
previous = page - 1
else:
previous = page
return render(request, 'blog/index.html',
{'article_list': page_article_list, 'page_num': range(1, page_num + 1), 'curr_page': page,
'next_page': next_page, 'previous': previous, 'top5_article_list': top5_article_list})
pass
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从零开始3小时Django开发个人博客系统title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous">script>
head>
<body>
<div class="container page-header">
<h1>从零开始3小时Django开发个人博客系统
<small>——来SCsmall>
h1>
div>
<div class="container page-body">
<div class="col-md-9" role="main">
<div class="body-main">
{% for article in article_list %}
<div>
<h2><a href="/blog/detail/{{ article.article_id }}">{{ article.title }}a>h2>
<p>
{{ article.brief_content }}
p>
div>
{% endfor %}
div>
<div class="body-footer">
<div class="col-md-4 col-md-offset-3">
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="/blog/index?page= {{ previous }}" aria-label="Previous">
<span aria-hidden="true">«span>
a>
li>
{% for num in page_num %}
<li><a href="/blog/index?page={{ num }}">{{ num }}a>li>
{% endfor %}
<li>
<a href="/blog/index?page= {{ next_page }}" aria-label="Next">
<span aria-hidden="true">»span>
a>
li>
ul>
nav>
div>
div>
div>
<div class="col-md-3" role="complementary">
<div>
<h2>最新文章h2>
{% for article in top5_article_list %}
<h4><a href="/blog/detail/{{ article.article_id }}">{{ article.title }}a>h4>
{% endfor %}
div>
div>
div>
body>
html>
from blog import views
urlpatterns = [
path('', views.get_index_page, name='home') # 自定义首页views,将空白路由定义为主页导向主页函数
]
{{ next_page }}" aria-label=“Next”>
»
from blog import views
urlpatterns = [
path('', views.get_index_page, name='home') # 自定义首页views,将空白路由定义为主页导向主页函数
]