心得:调试到半夜的结果,只想要完成它,别无他想
urls.py
from django.urls import path,re_path
from . import views
app_name = 'front'
urlpatterns = [
path('',views.index,name='index'),
path('main/',views.main,name='main'),
path('main/search/',views.search,name='search'),
path('main/add/',views.add,name='add'),
path('main/search/(?P<page>)/',views.detail,name='detail'),
]
views.py
from django.shortcuts import render,HttpResponse,reverse,redirect
from django.db import connection
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
@csrf_exempt
def index(request):
if request.method == 'GET':
return render(request,'front/index.html')
else:
cursor = connection.cursor()
username=request.POST.get('username')
password=request.POST.get('password')
res=cursor.execute("select password from userinfo where username=%s",[username])
if res != 0:
db_password=cursor.fetchone()[0] #输出值为元组,所以取第一个值
if password == db_password:
return redirect(reverse('front:main'))
else:
return render(request, 'front/index.html', context={'error': 'Password Error'})
else:
return render(request,'front/index.html',context={'error':'User Not Found'})
def main(request):
return render(request,'front/main.html')
def search(request):
cursor=connection.cursor()
cursor.execute("select id,name from books")
name=cursor.fetchall()
return render(request,'front/search.html',context={'name':name})
@csrf_exempt
def detail(request,page):
if request.method == "GET":
cursor = connection.cursor()
cursor.execute("select * from books where id=%s",[page])
book=cursor.fetchone()
return render(request,'front/detail.html',context={'book':book})
else:
res=request.POST.get('res')
if res == 'update':
cursor = connection.cursor()
name=request.POST.get('name')
author=request.POST.get('author')
content = request.POST.get('content')
cursor.execute("update books set name=%s,author=%s,content=%s where id=%s",[name,author,content,page])
elif res == 'delete':
cursor = connection.cursor()
cursor.execute("delete from books where id=%s",[page])
return redirect(reverse('front:search'))
@csrf_exempt
def add(request):
if request.method == "GET":
return render(request,'front/add.html')
else:
cursor = connection.cursor()
name = request.POST.get('name')
author = request.POST.get('author')
content = request.POST.get('content')
cursor.execute("insert into books(name,author,content) values(%s,%s,%s)",[name,author,content])
return render(request, 'front/add.html',context={'msg':'添加成功'})
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h2 style="color:red">{{ error }}</h2>
<br>
<br>
<form action="" method="post">
{% csrf_token %}
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
</table>
<br>
<input type="submit" value="登录">
</form>
</form>
</body>
</html>
main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h2 style="color:red">{{ error }}</h2>
<br>
<br>
<form action="" method="post">
{% csrf_token %}
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
</table>
<br>
<input type="submit" value="登录">
</form>
</form>
</body>
</html>
search.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>所有书本信息</title>
</head>
<body>
<ol>
{% for i in name %}
<li><a href="{% url 'front:detail' page=i.0 %}">{{ i.1 }}</a></li>
{% endfor %}
</ol>
<br>
<br>
<br>
<h3><a href="{% url 'front:main' %}">返回首页</a></h3>
</body>
</html>
add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新增书籍</title>
</head>
<body>
<h2 style="color:green">{{ msg }}</h2>
<form action="" method="post">
<table border="1">
<tr>
<td>书名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>内容:</td>
<td><input type="text" name="content"></td>
</tr>
</table>
<br>
<input type="submit" value="添加">
</form>
<br>
<br>
<br>
<h3><a href="{% url 'front:main' %}">返回首页</a></h3>
</body>
</html>
detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ book.1 }}</title>
</head>
<body>
<h2>书籍详细信息</h2><br>
<form action="" method="post">
<input type="hidden" name="res" value="update">
<table border="1">
<tbody>
<tr>
<td>书名:</td>
<td><input type="text" name="name" value={{ book.1 }}></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author" value={{ book.2 }}></td>
</tr>
<tr>
<td>内容:</td>
<td><input type="text" name="content" value={{ book.3 }}></td>
</tr>
</tbody>
</table>
<br>
<input type="submit" value="更新">
</form>
<br>
<form action="" method="post">
<input type="hidden" name="res" value="delete">
<input type="submit" value="删除">
</form>
<br>
<br>
<br>
<h3><a href="{% url 'front:main' %}">返回首页</a></h3>
</body>
</html>
图片: