A
A
Alexander2019-07-22 00:56:25
Python
Alexander, 2019-07-22 00:56:25

How to automatically fill Flask-WTF forms?

I have a database with links
I have a user profile in which I want to display in a table all the links associated with this user
I want the user to be able to change some columns of the database row through this table
I do it like this:
profile.html :

spoiler

{% extends 'base.html' %}

{% block content %}
<ul class"userdata">
  <li>Username: {{ current_user.username }}</li>
  <li>Email:  {{ current_user.email }}</li>
  <li><a href="#">Change password</a></li>
</ul>
<h3>Your links list</h3>
<table border="1">
  <tr>
    <th>shortlink</th>
    <th>stats</th>
    <th>privat</th>
  </tr>
  {% for link in current_user.links %}
  <tr>
    <form class="" method="post">
      {{ link_form.hidden_tag() }}
      <th>{{ link_form.shortlink() }}</th>
      <th>{{ link.count }}</th>
      <th>{{ link_form.privat_stats() }}</th>
      <th>{{ link_form.save() }}</th>
    </form>
  </tr>
  {% endfor %}
</table>
{% endblock %}

the tables in the database are made using SQLalchemy and look like this:
spoiler

from werkzeug.security import check_password_hash
from app import db, login_manager
from flask_login import UserMixin

@login_manager.user_loader
def load_user(user_id):
    return Users.query.get(user_id)

class Users(db.Model, UserMixin):

    __tablename__ = "Users"
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(64), unique = True, index = True)
    email = db.Column(db.String(64), unique = True, index = True)
    password = db.Column(db.String(120))

    links = db.relationship('Links', back_populates = 'user')    # two-side relationship with Links

    def check_password(self, password):
        return check_password_hash(self.password, password)

class Links(db.Model):

    __tablename__ = 'Links'
    id = db.Column(db.Integer, primary_key = True, unique = True)
    user_id = db.Column(db.Integer, db.ForeignKey('Users.id'))   # points to id in Users table
    longlink = db.Column(db.Text)
    shortlink = db.Column(db.Text)
    privat_stats = db.Column(db.Boolean)
    count = db.Column(db.Integer)

    user = db.relationship('Users', back_populates = 'links', uselist = False) # two-side relationship with Users

well, the code of the function responsible for the transition to the profile:
spoiler
@app.route('/profile')
@login_required
def profile():
    link_form = LINKform()
    if link_form.validate_on_submit():
        pass
    return render_template('profile.html', link_form = link_form)

in the browser it looks like this:
5d34deaf2f17a146047146.png
How to automatically fill the "shortlink" and "privat" fields with data from the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-07-22
@aleks0010

In your template, the link field code does not contain the value of this link.
Each field should be applied like this:
The value is taken from the link entry object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question