無法刪除新聞數據
new_dao
相关代码:
# coding:utf-8
from db.mysql_db import pool
# 查詢新聞表, 新聞類型表, 用戶表
class NewsDao():
# 查詢待審批新聞列表
def search_unreivewd_list(self, page):
try:
con = pool.get_connection()
cursor = con.cursor()
sql = 'SELECT n.id, n.title, t.type, u.username ' \
'FROM t_news n JOIN t_type t ON n.type_id=t.id ' \
'JOIN t_user u ON n.editor_id=u.id ' \
'WHERE n.state=%s ' \
'ORDER BY n.id DESC ' \
'LIMIT %s, %s'
# n.create_time
cursor.execute(sql, ('待審核', (page-1)*10, 10))
result = cursor.fetchall()
return result
except Exception as e:
print(e)
finally:
if 'con' in dir():
con.close()
# 查詢待審核新聞列表頁數
def search_unreviewed_count_page(self):
try:
con = pool.get_connection()
cursor = con.cursor()
sql = 'SELECT CEIL(COUNT(*)/10) FROM t_news WHERE state=%s' # 舉例: 如果有21條紀錄, 則顯示3頁, CEIL強制進位
cursor.execute(sql, ('待審核',))
count_page = cursor.fetchone()[0] # 因為結果集只有一條紀錄, 只有一個字段
return count_page
except Exception as e:
print(e)
finally:
if 'con' in dir():
con.close()
# 審核新聞 -> 將'待審核' 字段 改為 '已審核'
def update_unreviewed_news(self, id):
try:
con = pool.get_connection()
con.start_transaction()
cursor = con.cursor()
sql = 'UPDATE t_news SET state=%s WHERE id=%s'
cursor.execute(sql, ('已審核', id))
con.commit()
except Exception as e:
if 'con' in dir():
con.rollback()
print(e)
finally:
if 'con' in dir():
con.close()app.py
相关代码:
# coding:utf-8
# 管理使用平台的輸入輸出
import time
from colorama import Fore, Style
from getpass import getpass
from service.user_service import UserService
from service.news_service import NewsService
import os
import sys
__user_service = UserService()
__news_service = NewsService()
while True:
os.system('cls') # 清空指令
print(Fore.LIGHTBLUE_EX, "\n\t==========")
print("\n\t歡迎使用新聞管理系統")
print(Fore.LIGHTGREEN_EX, "\n\t==========")
print("\n\t1.登錄系統")
print("\n\t2.退出系統")
print(Style.RESET_ALL)
opt = input("\n\t輸入操作編號:") # 在這其中寫的數據將保存到opt裡, 變成str類型
if opt == '1':
username = input('\n\t請輸入用戶名:')
password = getpass('\n\t請輸入密碼:') # 隱去密碼, 不能使用input, 而是getpass
result = __user_service.login(username, password)
# 查詢角色
if result == True:
role = __user_service.search_user_role(username)
os.system('cls') # 二級菜單時, 系統選項應清空
while True: # 每一層都要死循環
if role == '新聞編輯':
print('test')
elif role == '管理員':
print(Fore.LIGHTBLUE_EX, '\n\t1.新聞管理')
print(Fore.LIGHTBLUE_EX, '\n\t2.用戶管理')
print(Fore.LIGHTGREEN_EX, "\n\tback.退出登錄")
print(Fore.LIGHTGREEN_EX, "\n\texit.退出系統")
print(Style.RESET_ALL)
choice = input('\n\t輸入操作編號:')
if choice == '1':
while True: # 三級菜單, 一樣需要死循環
os.system('cls')
print(Fore.LIGHTBLUE_EX, '\n\t1.審核新聞')
print(Fore.LIGHTBLUE_EX, '\n\t2.刪除新聞')
print(Fore.LIGHTGREEN_EX, "\n\tback.返回上一層")
print(Style.RESET_ALL) # 此使input顯示之語句為預設顏色
opt = input('\n\t請輸入操作編號:')
if opt == '1':
page = 1 # 必需要將變量設在循環外部, 否則每一次循環, 上一次搜索到的頁數將消失
while True:
os.system('cls')
count_page = __news_service.search_unreviewed_count_page() # 查詢總頁數
result = __news_service.search_unreviewed_list(page) # 所有紀錄的結果
# page 為當前頁數, 默認為第一頁
# 為何要使用索引的序號, 而不是主鍵值? 使新聞被刪除後, 不會主鍵顯示不連續
for index in range(len(result)):
one = result[index] # 需要逐一提取數據 -主鍵, title, content_id, type_id
print(Fore.LIGHTBLUE_EX, '\n\t%d\t%s\t%s\t%s' % (index+1, one[1], one[2], one[3]))
print(Fore.LIGHTBLUE_EX, '-----------------------------')
print('\n\t%d/%d' % (page, count_page))
print(Fore.LIGHTBLUE_EX, '-----------------------------')
print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
print(Fore.LIGHTRED_EX, "\n\tprev.上一頁")
print(Fore.LIGHTRED_EX, "\n\tnext.下一頁")
print(Style.RESET_ALL)
opt = input('\n\t請輸入操作編號:')
if opt == 'back':
break
elif opt == 'prev' and page > 1:
page -= 1 # 需不需要重新調用查詢功能? 不用! 因為page會保存在循環內, 到時會重新執行
elif opt == 'next' and page < count_page: # 當前頁數 < 總頁數
page += 1
elif int(opt) >= 1 and int(opt) <= 10: # 此處的opt要輸入控制台顯示的新聞序號, 一頁10條紀錄
# 但螢幕上看到的opt非原本的主鍵值, result[0]為原本結果的第一條紀錄
news_id = result[int(opt)-1][0]
__news_service.update_unreviewed_news(news_id)
elif opt == 'back':
break
# 二級菜單退出
elif choice == 'back':
break
elif choice == 'exit':
sys.exit(0)
else:
print('\n\t登陸失敗(3秒後自動返回)')
time.sleep(3)
elif opt == '2':
sys.exit(0)news_service.py
相关代码:
# coding:utf-8 from db.news_dao import NewsDao class NewsService(): __news_dao = NewsDao() # 查詢待審核新聞列表 def search_unreviewed_list(self, page): result = self.__news_dao.search_unreivewd_list(page) return result # 查詢待審核新聞列表頁數 def search_unreviewed_count_page(self): count_page = self.__news_dao.search_unreviewed_count_page() return count_page # 審核新聞 -> 將'待審核' 字段 改為 '已審核' def update_unreviewed_news(self, id): self.__news_dao.update_unreviewed_news(id)
10
收起
正在回答
1回答
同学,你好!
因为同学并未完成删除新闻部分代码。只写了审核新闻

祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
Python全能工程师
- 参与学习 人
- 提交作业 16416 份
- 解答问题 4469 个
全新版本覆盖5大热门就业方向:Web全栈、爬虫、数据分析、软件测试、人工智能,零基础进击Python全能型工程师,从大厂挑人到我挑大厂,诱人薪资在前方!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星