0
1 комментарий

Здравствуйте. Прошу помощи. Есть код(ниже), просто через IDLE он не хочет работать, ибо не может импортировать ImportError: cannot import name ‘FlaskForm’. Сначала не мог найти flask_wtf, но я скачал отдельно и установил, но вылезла эта проблема. Дальше хуже, скачал pycharm и попытался запустить через него. Всё шикарно GET, пишет Hi! по адресу, но как только через postman пытаюсь методом POST отправить что-то (либо просто инициализировать метод без данных) — выдаёт 500 ошибку (ниже её тоже приложил). Всё должно работать, ибо работает на видео, по которому я изучаю Flask, но у меня отказывается. Подскажите, будьте добры. Спасибо

[apcode language=»python»]

from flask import Flask, request
import json
from flask.json import jsonify
from flask_wtf import FlaskForm
from wtforms import StringField, validators
 class ContactForm(FlaskForm):
name = StringField(lavel = "Name", validators = [
validators.Length(min = 4, max = 25)
])
email = StringField(label = "E-mail", validators = [
validators.Length(min = 6, max = 35), validators.Email()
])
 app = Flask(__name__)
 @app.route("/form/user", methods = ["GET", "POST"])
def homm():
if request.method == "POST":
print(request.form)
form = ContactForm(request.form)
print(form.validate())
 if form.validate():
return ('valid', 200)
else:
return ('invalid', 400)
 if request.method == "GET":
return 'Hi!', 200
  if __name__ == '__main__':
app.run

[/apcode]

Текст ошибки:

[apcode language=»python»]

ImmutableMultiDict([('email', 'wauil@gmail.com'), ('paswword', 'fdgfdgdf')])
[2019-11-29 15:12:28,549] ERROR in app: Exception on /form/user [POST]
Traceback (most recent call last):
File "/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/rjdevera/test/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/rjdevera/flass.py", line 49, in homm
form = ContactForm(request.form)
File "/home/rjdevera/test/lib/python3.6/site-packages/wtforms/form.py", line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File "/home/rjdevera/test/lib/python3.6/site-packages/flask_wtf/form.py", line 88, in __init__
super(FlaskForm, self).__init__(formdata=formdata, **kwargs)
File "/home/rjdevera/test/lib/python3.6/site-packages/wtforms/form.py", line 272, in __init__
super(Form, self).__init__(self._unbound_fields, meta=meta_obj, prefix=prefix)
File "/home/rjdevera/test/lib/python3.6/site-packages/wtforms/form.py", line 52, in __init__
field = meta.bind_field(self, unbound_field, options)
File "/home/rjdevera/test/lib/python3.6/site-packages/wtforms/meta.py", line 27, in bind_field
return unbound_field.bind(form=form, **options)
File "/home/rjdevera/test/lib/python3.6/site-packages/wtforms/fields/core.py", line 353, in bind
return self.field_class(*self.args, **kw)
TypeError: __init__() got an unexpected keyword argument 'lavel'
127.0.0.1 - - [29/Nov/2019 15:12:28] "POST /form/user HTTP/1.1" 500 -

[/apcode]

Вот что отвечает сервер:

[apcode language=»xml»]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or
there is an error in the application.</p>

[/apcode]

Решенный вопрос
Voprosik добавлен комментарий

Добавил, спасибо, но проблему это не решило

ImmutableMultiDict([(’email’, ‘wauil@gmail.com’), (‘paswword’, ‘fdgfdgdf’)])
Traceback (most recent call last):
File «/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py», line 2446, in wsgi_app
response = self.full_dispatch_request()
File «/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py», line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File «/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py», line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File «/home/rjdevera/test/lib/python3.6/site-packages/flask/_compat.py», line 39, in reraise
raise value
File «/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py», line 1949, in full_dispatch_request
rv = self.dispatch_request()
File «/home/rjdevera/test/lib/python3.6/site-packages/flask/app.py», line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File «/home/rjdevera/flass.py», line 49, in homm
form = ContactForm(request.form)
File «/home/rjdevera/test/lib/python3.6/site-packages/wtforms/form.py», line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File «/home/rjdevera/test/lib/python3.6/site-packages/flask_wtf/form.py», line 88, in __init__
super(FlaskForm, self).__init__(formdata=formdata, **kwargs)
File «/home/rjdevera/test/lib/python3.6/site-packages/wtforms/form.py», line 278, in __init__
self.process(formdata, obj, data=data, **kwargs)
File «/home/rjdevera/test/lib/python3.6/site-packages/wtforms/form.py», line 132, in process
field.process(formdata)
File «/home/rjdevera/test/lib/python3.6/site-packages/wtforms/csrf/core.py», line 43, in process
self.current_token = self.csrf_impl.generate_csrf_token(self)
File «/home/rjdevera/test/lib/python3.6/site-packages/flask_wtf/csrf.py», line 134, in generate_csrf_token
token_key=self.meta.csrf_field_name
File «/home/rjdevera/test/lib/python3.6/site-packages/flask_wtf/csrf.py», line 35, in generate_csrf
message=’A secret key is required to use CSRF.’
File «/home/rjdevera/test/lib/python3.6/site-packages/flask_wtf/csrf.py», line 121, in _get_config
raise KeyError(message)
KeyError: ‘A secret key is required to use CSRF.’

Добавить комментарий