Уведомления

Группа в Telegram: @pythonsu

#1 Фев. 23, 2020 15:04:36

Bazz
Зарегистрирован: 2020-02-23
Сообщения: 1
Репутация: +  0  -
Профиль   Отправить e-mail  

Проблема с кодом по книге Head First Python

Добрый день, изучаю Питон по книге Head First Python. Дошел до главы по изучению Flask и MySQL. Код полностью соответствует книге, но выбивает ошибку. Помогите разобраться с ошибкой.

File “/Users/a1502/Downloads/hfpy2e-all/ch09/webapp/vsearch4web.py”, line 36, in do_search
log_request(request)
File “/Users/a1502/Downloads/hfpy2e-all/ch09/webapp/vsearch4web.py”, line 25, in log_request
req.user_agent.browser,


from flask import Flask, render_template, request, escape
from vsearch import search4letters

from DBcm import UseDatabase

app = Flask(__name__)

app.config = {'host': ‘127.0.0.1’,
‘user’: ‘vsearch’,
‘password’: ‘122345’,
‘database’: ‘vsearchlogDB’, }


def log_request(req: ‘flask_request’, res: str) -> None:
“”“Log details of the web request and the results.”“”

with UseDatabase(app.config) as cursor:
_SQL = “”“insert into log
(phrase, letters, ip, browser_string, results)
values
(%s, %s, %s, %s, %s)”“”
cursor.execute(_SQL, (req.form,
req.form,
req.remote_addr,
req.user_agent.browser,
res, ))


@app.route('/search4', methods
def do_search() -> ‘html’:
“”“Extract the posted data; perform the search; return results.”“”
phrase = request.form
letters = request.form
title = ‘Here are your results:’
results = str(search4letters(phrase, letters))
log_request(request)
log_request(results)
return render_template('results.html',
the_title=title,
the_phrase=phrase,
the_letters=letters,
the_results=results,)


@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.route('/viewlog')
def view_the_log() -> ‘html’:
“”“Display the contents of the log file as a HTML table.”“”
with UseDatabase(app.config) as cursor:
_SQL = “”“select phrase, letters, ip, browser_string, results
from log”“”
cursor.execute(_SQL)
contents = cursor.fetchall()
titles = ('Phrase', ‘Letters’, ‘Remote_addr’, ‘User_agent’, ‘Results’)
return render_template('viewlog.html',
the_title='View Log',
the_row_titles=titles,
the_data=contents,)


if __name__ == ‘__main__’:
app.run(debug=True)

Прикреплённый файлы:
attachment Снимок экрана 2020-02-23 в 16.00.46.png (401,2 KБ)

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version