1. 程式人生 > >對於Flask的更新處理

對於Flask的更新處理

對原有的程式碼進行了更新,並加入新的HTML頁面,關注註釋和理解等有空了在補上,現在天天跑工地挺累的

對vsearch4web頁面實行新增在嗎
from flask import Flask, render_template, request, escape
from vsearch import search4letters

app = Flask(__name__)


def log_request(req: 'flask_request', res: str) -> None:
    with open('vsearch.log', 'a') as log:
        
# print(req, res, file=log) # print(str(dir(req)),res,file=log) print(req.form, req.remote_addr, req.user_agent, res, file=log, sep='|') @app.route('/search4', methods=['POST']) def do_search() -> 'html': """Extract the posted data; perform the search; return results.""" phrase
= request.form['phrase'] letters = request.form['letters'] title = 'Here are your results:' results = str(search4letters(phrase, letters)) log_request(request, results) return render_template('results.html', the_title=title, the_phrase
=phrase, the_letters=letters, the_results=results, ) @app.route('/viewlog') def view_the_log() -> 'html': contents = [] with open('vsearch.log')as log: # contents = log.read() # contents=log.readlines() for line in log: contents.append([]) for item in line.split('|'): # contents.append(escape(item)) contents[-1].append(escape(item)) titles = ('Form Data', 'Remote_addr', 'User_agent', 'Results') print(titles) print(contents) return render_template('viewlog.html', the_title='View Log', the_row_titles=titles, the_data=contents, ) @app.route('/') @app.route('/entry') def entry_page() -> 'html': """Display this webapp's HTML form.""" return render_template('entry.html', the_title='Welcome to search4letters on the web!') app.run

 

新的介面在templates中加入viewlog

{% extends 'base.html' %}

{% block body %}

    <h2>{{ the_title }}</h2>

    <table>
        <tr>
            {% for row_title in the_row_titles %}
                <th>{{ row_title }}</th>
            {% endfor %}
        </tr>
        {% for log_row in the_data %}
            <tr>
                {% for item in log_row %}
                    <td>{{ item }}</td>
                {% endfor %}

            </tr>
        {% endfor %}
    </table>

{% endblock %}

結果介面