Skip to content
Advertisement

Name from other page included in other page is not displayed

In my application, I would like to include the name, entered by the user, at page one, to the second page. But when I paste the jinja print “{{ data }}” to the table, the name does not appear on the second page. It works flawlessly on the first one. How can I make the name appear on both sides? Here is my code: Tables:

class Earning(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    earning_date = db.Column(db.DateTime(), default=func.now())
    earning_data = db.Column(db.String())
    earning_amount = db.Column(db.Integer())
    note_id = db.Column(db.Integer, db.ForeignKey('note.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    data = db.Column(db.String(10000))
    description = db.Column(db.String(10000))
    date = db.Column(db.Date)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    earnings = db.relationship('Earning', backref='note', lazy='select')
    spendings = db.relationship('Spending', backref='note')

creating 1st page, where user enter the name:

@views.route('/', methods=['GET', 'POST'])
@login_required
def home():
    
    if request.method == 'POST':
        note=request.form.get('note')
        description = request.form.get('description')
        if len(note) < 1:
            flash('Nie wpisałeś żadnej treści', category='error')
        else:
            new_note = Note(description=description, date=date.today(), data=note, user_id=current_user.id)
            db.session.add(new_note)
            db.session.commit()
            flash('Budżet dodany', category='success')

    return render_template("home.html", user=current_user)

creating the second page where I want to display the name entered by user at home.html:

@views.route('/earnings', methods=['GET', 'POST'])
@login_required
def earnings():

    if request.method == 'POST':
        earning_data = request.form.get('earning_data')
        earning_amount = request.form.get('earning_amount')
        if len(earning_data) < 1:
            flash('Nie wpisałeś żadnej treści', category='error')
        elif len(earning_amount) < 1:
            flash('Nie wpisałeś żadnej wartości', category='error')
        else:
            earning_table = Earning(
            earning_data=earning_data, 
            earning_amount=earning_amount, 
            user_id=current_user.id,
            )   
            db.session.add(earning_table)
            db.session.commit()
            flash('Zarobek dodany', category='success')

    workingData = Earning.query.all()
    earning_list = []
    for sales in workingData:
        i = sales.earning_amount
        earning_list.insert(0,i)
    earning_list = sum(earning_list)
    
    return render_template('earnings.html',
    earning_list=earning_list,
    user=current_user,
    )

and HTML which:

<main class="main">
  <div class="responsive-wrapper">
  <div class="main-header">
    <h1> <i> NAME: </i> {{ data }} </h1>
  </div> 

{{ data }} works fine at first page home.html, but at earnings.html is not displayed Please help

Advertisement

Answer

You need to pass the variable to the the page here:

 return render_template("home.html", user=current_user)

I’m not sure where you get that variable though on that page, I can’t see it being sent. Additionally, it looks like you are validating the user input in the form that it meets a length criteria, you are better doing this using forms validation using WTF forms.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement