1、views.py文件:
from django.shortcuts import render
from app1.models import Blogs
# Create your views here.
# 引入重定项模块
from django.http import HttpResponseRedirect
def gotoIndex(request):
return render(request,'index.html')
# 保存内容
def addDatas(request):
title=request.POST.get('title')
concent=request.POST.get('concent')
if Blogs(title=title,concent=concent):
Blogs(title=title,concent=concent).save()
return render(request,'index.html')
# 展示内容
def show(request):
# 获取全部数据
DatasAll=Blogs.objects.all()
print(DatasAll)
return render(request,'index.html',{'DatasAll':DatasAll})
# 跳转到修改界面
def gotoUpdateData(request):
ID=request.GET.get('ID')
Datas=Blogs.objects.get(ID=ID)
return render(request,'updateDatas.html',{'Datas':Datas})
# 回到展示页面
def gobackshow(request):
return render(request,'index.html')
# 修改内容
def updateData(request):
title=request.GET.get('title')
concent=request.GET.get('concent')
ID=request.GET.get('ID')
# 修改
Blogs.objects.filter(ID=ID).update(title=title,concent=concent)
# 获取数据应该在修改数据之后
Datas=Blogs.objects.get(ID=ID)
return render(request,'updateDatas.html',{'msg':'修改成功','Datas':Datas,})
# 删除单条数据
def deleteDate(request):
title=request.GET.get('title')
concent=request.GET.get('concent')
ID=request.GET.get('ID')
Datas=Blogs.objects.get(ID=ID)
Blogs.objects.filter(ID=ID).delete()
# return HttpResponseRedirect('/index.html/')
DatasAll=Blogs.objects.all()
return render(request,'index.html',{'DatasAll':DatasAll})
# 删除多条数据
def doDeleteAll(request):
checkstr=request.GET.get('checkstr')
Blogs.objects.extra(where=['ID in ('+checkstr+')']).delete()
DatasAll=Blogs.objects.all()
return render(request,'index.html',{'DatasAll':DatasAll})
2、models.py文件:(配置生成数据库)
class Blogs(models.Model):
ID=models.AutoField('ID',primary_key=True)
title=models.CharField('title',max_length=100)
concent=models.TextField('concent',max_length=1000)
3、settings.py文件:(配置数据库连接,使用sqlite3数据库)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
4、urls.py 文件:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from app1 import views as app
urlpatterns = [
url('admin/', admin.site.urls),
url('^$', app.gotoIndex),
url('addDatas', app.addDatas),
url('show', app.show),
url('gotoUpdateData', app.gotoUpdateData),
url('gobackshow', app.gobackshow),
url('updateData', app.updateData),
url('deleteDate', app.deleteDate),
url('doDeleteAll', app.doDeleteAll),
]
5、html 文件:
(1)index.html
<body style="background:url({% static 'img/1.jpg' %}); background-size: 100% 100%; ">
<div>
<h1>个人博客</h1>
<!-- 查看内容 -->
<form action="/show/" method="get">
{% csrf_token %}
<input type="submit" value="查看已发表内容" >
<input type="button" id="allDelete" value="批量删除">
<input id="allCheck" type="button" value="全选" >
<input id="reverseCheck" type="button" value="反选" ><br>
{% for i in DatasAll %}
<div style=" height:auto; border: 1px solid blue;">
<p style="color: white;">第{{i.ID}}篇文章</p>
<p style="color: white; text-align: center;">标题:{{i.title}}</p>
<p style="color: white;">内容:{{i.concent}}</p>
<input type="checkbox" class="check" value="{{i.ID}}">
<input type="button" onclick="javascript:gotoUpdateDatas({{i.ID}})" value="修改" >
<input type="button" onclick="javascript:deleteDatas({{i.ID}})" value="删除" >
</div><br>
{% endfor %}
</form>
<!-- 发表内容 -->
<form action="/addDatas/" method="post">
{% csrf_token %}
<table border="1" width="300">
<tr>
<th >
<input id="title" type="text" name="title" placeholder="请输入标题">
</th>
</tr>
<tr>
<td>
<textarea id="concent" name="concent" placeholder="请输入内容:"></textarea>
</td>
</tr>
</table>
<input type="submit" value="提交" >
</form>
</div>
</body>
————————-js文件—————————-
js链接代码
{% load staticfiles %}
<script type="text/javascript" src="{% static 'js/javascript.js' %}"></script>
function gotoUpdateDatas(ID){
location="/gotoUpdateData/?ID="+ID
}
function deleteDatas(ID){
if(confirm('是否删除'+ID+'?')){
// 地址栏传参调用python中删除函数
location="/deleteDate/?ID="+ID
alert("删除成功!")
}
}
var allDelete=document.getElementById('allDelete'),
allCheck=document.getElementById('allCheck'),
reverseCheck=document.getElementById('reverseCheck'),
check=document.getElementsByClassName('check');
//全选按钮点击事件
allCheck.onclick=function(){
for(var i=0;i<check.length;i++){
check[i].checked=true;
}
}
reverseCheck.onclick=function(){
for(var i=0;i<check.length;i++){
if(check[i].checked==true){
check[i].checked=false
}
else{
check[i].checked=true
}
}
}
allDelete.onclick=function(){
var str1='';
// 获取所选中内容的id
for(var i=0;i<check.length;i++){
if(check[i].checked==true){
str1+=check[i].value+',';
}
}
// 截取字符串,去掉尾部逗号
var checkstr=str1.substring(0,str1.length-1);
if(checkstr==''){
alert('无选中内容,请选择要删除的项!')
}else{
if(confirm('确定删除所选项?')){
location='/doDeleteAll/?checkstr='+checkstr
alert('删除成功!')
}
}
}
(2)updateDatas文件
<body style="background:url({% static 'img/2.jpg' %}); background-size: 100% 100%; ">
<form action="/updateData/" method="get">
{% csrf_token %}
<input type="hidden" name="ID" value="{{Datas.ID}}">
<div>
<h1>修改内容</h1>
<table border="1" width="300">
<tr>
<th>
<input id="title" type="text" name="title" value="{{Datas.title}}">
</th>
</tr>
<tr>
<td>
<textarea id="concent" name="concent">{{Datas.concent}}</textarea>
</td>
</tr>
</table>
<input id="submit1" type="submit" value="提交" >
<input type="button" onclick="javascript:gobackshows()" value="返回上一层">
</div><br>
</form>
{{msg}}
</body>
————————-js文件—————————-
function gobackshows(){
location="/gobackshow/?"
}
var submit1=document.getElementById('submit1')
submit1.onclick=function(){
alert("修改成功!")
}
(3)两个html文件链接了一个Css文件
————————-css文件—————————-
CSS链接代码:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/CSS.css' %}">
div{
width: 500px;
height:1000px;
margin: 0 auto;
}
#title{
width: 496px;
height:36px;
}
#concent{
width: 496px;
height:156px;
}
h1{
color: red;
text-align: center;
}