flask-wtforms

jinja2.exceptions.UndefinedError: 'str object' has no attribute 'username'

这一生的挚爱 提交于 2019-12-11 09:37:03
问题 I am having issues getting my flask app to recognize my form variables when trying to load the register.html page. I have tried to reload my virtualenv and have tried to load the template code from another person and I get the same error. Here is the traceback I am getting. File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 2295, in wsgi_app

Update model with WTForms form data

本秂侑毒 提交于 2019-12-11 08:27:17
问题 I have some Flask-SQLAlchemy models and Flask-WTF forms generated with wtforms_alchemy to represent them. I implemented a method on each model to update its attributes from a form's data. For each new model and field I have to update these methods, which is annoying. Is there a way to make this more automatic, or a a feature in the libraries I'm using that I'm missing? def edit_car(car_id): form = CarForm(request.form) if form.is_valid(): car = Car.query.get_or_404(car_id) car.from_form(form)

More specific SQL query with flask-wtf queryselectfield

天涯浪子 提交于 2019-12-11 07:44:39
问题 I'd like to create a Login-Form using Flask and WTF. The application should read the nicknames of all users out of a database-table and display them in a QuerySelectField. It does work - but I think that I can do it in a 'nicer way'... I have a database table like this: id | nickname | fullname | email ----+-----------+----------------+-------------------------- 1 | Admin | The Admin | admin@example.com 2 | JDoe | John Doe | jd@example.com In my models.py I have the corresponding class User

Generating a CSRF token manually with Flask WTF-Forms

荒凉一梦 提交于 2019-12-11 07:34:07
问题 I'd like to create and fill out a Flask WTF-Form using only python code. However, the form doesn't automatically generate a CSRF token when I create it with python code. Is there any way to do this manually? The form in question: from flask_wtf import Form from wtforms import StringField from wtforms.validators import DataRequired, URL class URLForm(Form): url = StringField('url', validators=[DataRequired(), URL(), Level3Url()]) the code I use to generate the form: from forms import URLForm

Post/Redirect/Get pattern in flask

二次信任 提交于 2019-12-11 07:30:45
问题 The view function of my toy app was: @app.route('/', methods=['GET', 'POST']) def index(): name = None form = NameForm() if form.validate_on_submit(): name = form.name.data form.name.data = '' return render_template('index.html', form=form, name=name) And it looks like this when I use PRG: @app.route('/', methods=['GET', 'POST']) def index(): form = NameForm() if form.validate_on_submit(): session['name'] = form.name.data return redirect(url_for('index')) return render_template('index.html',

Python/Flask-WTF - How can I randomize the choices from dynamic RadioField?

我与影子孤独终老i 提交于 2019-12-11 07:30:19
问题 I am looking to build a multiple choice quiz using python/flask/flask-wtf. I am successfully able to pull random questions from a database as well as pull random choices as the possible answers. I am using a for loop on the template page that first displays the question and then the possible answers. Here is my template. <div class="quote-block"> {% for quote in quotes %} {{ quote.line_text }} <form method="POST"> {{ form.hidden_tag() }} {{ form.question }} <input type="submit"> </form> {%

Flask - Populate SelectField choices with array

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:08:18
问题 Newbie here, been writing Python scripts for little bit more than 6 months. I'm trying to populate a wtf SelectField with a list, returned from a function that fetches the data from Slack API. The list contains channel names, that i wanted to setup as the choices of the SelectField. Here's the code of my function: def get_channels_list(slack_token): sc = SlackClient(slack_token) a = sc.api_call('channels.list', exclude_archived=1, exclude_members=1,) a = json.dumps(a) a = json.loads(a) list1

How access WTForm with JQuery

有些话、适合烂在心里 提交于 2019-12-11 06:19:29
问题 I would like to ask for help with accessing WTForm fields: I have following form: class model_bolt_InputForm(FlaskForm): # Bolt Inputs bolt_size = SelectField('Bolt size [mm]', choices=[('M6', 'M6'),('M8', 'M8'),('M10', 'M10'), ('M12', 'M12'), ('M16', 'M16'), ('M20', 'M20'), ('M24', 'M24'), ('M30', 'M30'), ('M36', 'M36'), ('M42', 'M42'), ('M48', 'M48'), ('M56', 'M56'), ('M64', 'M64')], default='M12') bolt_grade = SelectField('Bolt grade [-]', choices=[('4.6','4.6'), ('4.8','4.8'), ('5.6','5.6

how can i use a string to to represent an sqlalchemy object attribute?

允我心安 提交于 2019-12-11 06:05:07
问题 I am writing a Flask application with SQLalchemy and WTForms. Trouble with database updates... I have many object table fields I am trying to update. so I started with this... # this works great but needs many lines of code event.evt_num = form.evt_num.data event.evt_name = form.evt_name.data event.evt_location = form.evt_location.data event.evt_address = form.evt_address.data event.evt_city = form.evt_city.data event.evt_state = form.evt_state.data ... While the above snippet works, in

How to populate a submit popup based on the value you filled to evaluate a condition | flask

女生的网名这么多〃 提交于 2019-12-11 04:46:46
问题 I have a jinja html template which has a form. The form is a wtforms , assume it like >>> from wtforms import Form, BooleanField, IntegerField, SelectField, StringField, validators >>> class COOLForm(Form): action = SelectField('Action', description='Action', choices=[('online', 'ONLINE'), ('offline', 'OFFLINE')]) ... The html template looks something like <div class = "major" <form id="blah"> <input type="hidden"> {{ forms.form_field(coolform.action) }} . . </div> Then in the same file i