-1
我用Bottle:Python web框架創建了一個小型wiki頁面。現在每件事情都很好。您可以創建一篇文章,方法是轉到「創建一篇新文章」並給它一個標題並寫下一些文本。然後,所有創建的文章將顯示在列表中的索引頁面上,您可以單擊它們以打開並閱讀。使用Python的小型網頁
但事情是當我點擊文章的目的是通過給它一個新的標題並添加一些新的文字來編輯文章。它不會更改原始文本文件的名稱,而是會得到一個帶有新標題的新文本文件,並且原始文本文件仍然存在。
這是代碼:
from bottle import route, run, template, request, static_file
from os import listdir
import sys
host='localhost'
@route('/static/<filname>')
def serce_static(filname):
return static_file(filname, root="static")
@route("/")
def list_articles():
'''
This is the home page, which shows a list of links to all articles
in the wiki.
'''
files = listdir("wiki")
articles = []
for i in files:
lista = i.split('.')
word = lista[0]
lista1 = word.split('/')
articles.append(lista1[0])
return template("index", articles=articles)
@route('/wiki/<article>',)
def show_article(article):
'''
Displays the user´s text for the user
'''
wikifile = open('wiki/' + article + '.txt', 'r')
text = wikifile.read()
wikifile.close()
return template('page', title = article, text = text)
@route('/edit/')
def edit_form():
'''
Shows a form which allows the user to input a title and content
for an article. This form should be sent via POST to /update/.
'''
return template('edit')
@route('/update/', method='POST')
def update_article():
'''
Receives page title and contents from a form, and creates/updates a
text file for that page.
'''
title = request.forms.title
text = request.forms.text
tx = open('wiki/' + title + '.txt', 'w')
tx.write(text)
tx.close()
return template('thanks', title=title, text=text)
run(host='localhost', port=8080, debug=True, reloader=True)