1.在前面的基础上添加删除文章按钮,作者和创作时间
1.修改models文件,在article的model中添加时间和作者
from django.db import models
# Create your models here.
from django.db import models
# Create your models here.
class ListInfo(models.Model):
articleName=models.CharField(max_length=20)
def __str__(self):
return self.articleName
class ArticleInfo(models.Model):
articleTitle=models.CharField(max_length=20)
article=models.CharField(max_length=10000)
gender=models.BooleanField(default=True)
list=models.ForeignKey(ListInfo,on_delete=models.CASCADE)
author=models.CharField(max_length=20)
create_date=models.DateField()
2.添加删除文章的方法和url
url
from . import views
from django.conf.urls import url
app_name="[blogApp]"
urlpatterns=[
#i索引的url
url("index",views.index,name="index"),
#文章的url
url("article/(\d+)",views.details,name="article_page"),
#修改文章跳转的url
url("change/(\d+)",views.change,name="change_article"),
#创建文章跳转的url
url("create",views.create_article,name="create_article"),
#创建新文章的url
url("edit/action",views.edit_action,name="edit_action"),
#修改文章的url
url("action",views.change_action,name="change_action"),
#删除文章的url
url("delete/(\d+)",views.del_action,name="del_article")
]
添加删除的方法
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
from django.db.models import Max
from time import strftime, gmtime
Create your views here.
def index(request):
#索引的方法,获取articleList表的值,
# 将值传入到index.html中进行输出
list=ListInfo.objects.all()
#redner传入的参数content是以kv对的形式传入的
context={“articleList”:list}
return render(request,“blog/index2.html”,context)
def details(request,id):
#通过index中标题的id得到article的id,
# 获取articleList的id,然后传入到html进行输出
list=ListInfo.objects.get(id=id).articleinfo_set.all()
context={“articleList”:list}
return render(request,‘blog/article/article.html’,context)
#点击修改文章时,传入的id,
#将文章的内容传入到修改的网页中
def change(request,id):
list=ArticleInfo.objects.get(id=id)
context={“article”:list}
return render(request,“blog/article/changeArticle.html”,context)
#创建新文章时的方法
def edit_action(request):
#print(ArticleInfo.objects.all().aggregate(Max(“id”)))
#print(ArticleInfo.objects.all().aggregate(Max(“id”)).get(“id__max”))
#先获取两个表的最大的id值,方便传入
articleMax=ArticleInfo.objects.all().aggregate(Max(“id”)).get(“id__max”)
listMax= ListInfo.objects.all().aggregate(Max(“id”)).get(“id__max”)
#获取文章修改的input的字段
title=request.POST.get(“title”,“TITLE”)
author=request.POST.get(“author”,“AUTHOR”)
content=request.POST.get(“article”,“ARTICLE”)
#获取当前时间
create_date= strftime("%Y-%m-%d", gmtime())
#然后将文章修改到两个表中
#判断输入的字段是否为空
if(title.isspace() or author.isspace() or content.isspace()):
return render(request, “blog/article/create_error.html”)
else:
add = ArticleInfo(articleTitle=title, article=content, id=articleMax + 1, list_id=listMax + 1, author=author,
create_date=create_date)
add1 = ListInfo(articleName=title, id=listMax + 1)
# 保存
add1.save()
add.save()
#获取索引表的数据,返回到index中
articles=ListInfo.objects.all()
context={“articleList”:articles}
return render(request,“blog/index2.html”,context)
#创建新文章的按钮,跳转到创建新文章的网页
def create_article(request):
return render(request,“blog/article/createArticle.html”)
#更改文章方法,更改网页点击提交,
#之后将文章修改内容保存到两个表的方法
def change_action(request):
#获取原先文章的id
retid=request.POST.get(“articleId”,“ARTICLEID”)
print(retid)
#获取文章的修改的内容
title = request.POST.get(“title”, “TITLE”)
content = request.POST.get(“article”, “ARTICLE”)
article=ArticleInfo.objects.get(id=retid)
article.articleTitle=title
article.article=content
#更改修改时间
article.create_date=strftime("%Y-%m-%d" , gmtime())
list=ListInfo.objects.get(id=article.list_id)
list.articleName=title
#保存
list.save()
article.save()
#返回到索引表
articles=ListInfo.objects.all()
context={“articleList”:articles}
return render(request, “blog/index2.html”, context)
#删除文章的方法
def del_action(request,id):
#获取文章
article=ArticleInfo.objects.get(id=id)
#获取索引
list = ListInfo.objects.get(id=article.list_id)
#删除索引和文章
article.delete()
list.delete()
#返回到索引
articles = ListInfo.objects.all()
context = {“articleList”: articles}
return render(request, “blog/index2.html”, context)
3.更改文章显示的html,添加删除按钮,作者和创作时间的显示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
{% load static %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jisuanji</title>
<style type="text/css">
.box1{
background:url("{% static "img2/shutterstock_16717870.jpg"%}") no-repeat;
height:100%;
}
.box1 span{
font-size:50px;
color:#000;
display:inline-block;
margin:20px auto;
text-align:center;
width:100%;}
.box2{
padding: 0px;
height: 500px;
width: 100%;
margin:0px;
background:#6C3;
font-size:20px;
display:inline-block;
}
.p{
text-indent:2em;
color:#000;}
.box3{
font-size: 18px;
margin-top: 0px;
padding: 0px;
text-align: center;
background:url("{% static "img2/bg-0782.gif"%}");
height:125px;}
.article{
text-indent: 2em;
color: brown;
}
.words{
display: inline-block;
color: crimson;
}
.author{
margin-left: 55%;
}
</style>
</head>
<body>
<div class="box1">
{%for article in articleList%}
<span>{{article.articleTitle}}</span>
<a>{{article.create_date}}</a>
<a class="author">{{article.author}}</a>
</div>
<div class="box2">
<p class="article">
{{article.article}}
</p>
</div>
<div class="box3">
<br>
<a href="{% url 'blogApp:change_article' article.id%}" class="words">修改当前文章</a><br>
<a href="{% url 'blogApp:del_article' article.id%}" class="words">删除改文章</a><br>
<a href="index" class="words"> 返回博客主页</a>
{%endfor%}
</div>
</body>
</html>
同理更改修改文章和创建文章的html
修改文章的html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
{% load static %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jisuanji</title>
<style type="text/css">
.title{
font-size: 20px;
background:url("{% static "img2/shutterstock_16717870.jpg"%}") no-repeat;
height:110px;
text-align: center;
}
.article{
padding: 0px;
height: 500px;
width: 100%;
margin:0px;
background:#6C3;
font-size:20px;
display:inline-block;
text-align: center;
}
.background3{
font-size: 18px;
margin-top: 0px;
padding: 0px;
text-align: center;
background:url("{% static "img2/bg-0782.gif"%}");
height:125px;
}
#title{
vert-align: top;
width: 50%;
height: 50px;
}
#article{
width: 90%;
height: 90%;
}
#false{
width: 10%;
height: 30%;
}
#sure{
width: 10%;
height: 30%;
}
.author{
margin-left: 55%;
}
</style>
</head>
<body>
<form action="{%url 'blogApp:change_action'%}" method="post">
<input type="hidden" id="articleId" name="articleId" value="{{article.id}}"/>
<div class="title">
<span>
<a>文章标题</a><br>
<input type="text" id="title" name="title" value="{{article.articleTitle}}"/>
</span></input>
<a>{{article.create_date}}</a>
<a class="author">{{article.author}}</a>
</div>
<div class="article">
<a>文章内容</a><br>
<textarea id="article" name="article">{{article.article}}</textarea>
</div>
<div class="background3">
<br>
<br>
<input type="submit" id="sure" name="{{article.id}}" value="确定"/>
<input type="button" id="false" name="false" value="取消"onclick="window.open('index.html')"/>
</div>
</form>
</body>
</html>
创建文章的html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
{% load static %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jisuanji</title>
<style type="text/css">
.title{
font-size: 20px;
background:url("{% static "img2/shutterstock_16717870.jpg"%}") no-repeat;
height:110px;
text-align: center;
}
.article{
padding: 0px;
height: 500px;
width: 100%;
margin:0px;
background:#6C3;
font-size:20px;
display:inline-block;
text-align: center;
}
.background3{
font-size: 18px;
margin-top: 0px;
padding: 0px;
text-align: center;
background:url("{% static "img2/bg-0782.gif"%}");
height:125px;
}
#title{
vert-align: top;
width: 50%;
height: 50px;
}
#article{
width: 90%;
height: 90%;
}
#false{
width: 10%;
height: 30%;
}
#sure{
width: 10%;
height: 30%;
}
.author{
margin-left: 15%;
}
</style>
</head>
<body>
<form action="{%url 'blogApp:edit_action'%}" method="post">
<div class="title">
<span>
<a>文章标题</a><br>
<input type="text" id="title" name="title" value="">
</span><br>
<a class="author">作者</a>
<input type="text" id="author" name="author" value="">
</div>
<div class="article">
<a>文章内容</a><br>
<textarea id="article" name="article"></textarea>
</div>
<div class="background3">
<br>
<br>
<input type="submit" id="sure" name="sure" value="确定">
<input type="button" id="false" name="false" value="取消"onclick="window.location.href='index2.html'">
</div>
</form>
</body>
</html>
后台管理
创建超级用户
python manage.py createsuperuser
Username (leave blank to use ‘a’): admin
Email address: admin@oracle.com
Password:
Password (again):
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
python manage.py runserver 9000
在网址中输入127.0.0.1:9000访问
然后登录
在models中添加显示的方法的model类中添加
def str(self):
return self.articleName
在admin中修改后台管理显示
from django.contrib import admin
# Register your models here.
from .models import *
class ListInfoAdmin(admin.ModelAdmin):
list_display = ["id","articleName"]
list_filter = ("articleName",)
class ArticleInfoAdmin(admin.ModelAdmin):
#如何显示
list_display = ["id","articleTitle","article","gender","author","create_date"]
#添加关于“create_date”这个字段的过滤器
list_filter = ("create_date",)
admin.site.register(ListInfo,ListInfoAdmin)
admin.site.register(ArticleInfo,ArticleInfoAdmin)
具体如下