2016-10-11 4 views
-1

Я создал небольшую wiki-страницу с помощью Бутылки: веб-фреймворк Python. Сейчас все отлично работает. Вы создаете статью, перейдя в «Создать новую статью» и давая ей название и записывая текст. Затем вся созданная статья отображается на индексной странице в списке, и вы можете щелкнуть по ней, чтобы открыть и прочитать.Маленькая веб-страница с 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) 

ответ

0

В статье объект слишком прост для отображения, редактирования или обновления.

  1. Статья должна иметь ID как его имя файла.
  2. Файл статьи должен содержать два поля: заголовок и текст.

    Например: 10022.txt

    title: 
    bottle 
    text: 
    Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. 
    
  3. Вы должны получить статью ID.

  4. Вы можете открыть файл по ID и изменить его название и текст.