Ich habe eine kleine Wiki-Seite mit Bottle: Python Web Framework erstellt. Im Moment funktioniert alles gut. Sie erstellen einen Artikel, indem Sie zu "Einen neuen Artikel erstellen" gehen und ihm einen Titel geben und einen Text aufschreiben. Dann wird der gesamte erstellte Artikel auf der Indexseite in einer Liste angezeigt, und Sie können auf sie klicken, um sie zu öffnen und zu lesen.Kleine Webseite mit Python
Aber die Sache ist, wenn ich auf den Artikel mit dem Zweck klicke, den Artikel zu redigieren, indem ich ihm einen neuen Titel gebe und einige neue Wörter dem Text hinzufüge. Es ändert nicht den Namen der ursprünglichen Textdatei, stattdessen erhalte ich eine neue Textdatei mit dem neuen Titel und die ursprüngliche Textdatei bleibt erhalten.
Dies ist der Code:
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)