0

I am trying to create a journal app in Python Flask. I am stuck trying to send different data to my functions. It is hard to describe without showing you the code so I will post that below then continue.

from flask import Flask, render_template

app = Flask(__name__)

entries = [
    {
        'entry_number': 1,
        'author': 'X',
        'title': 'First Post',
        'date_written': 'January 1st, 2019',
        'content': 'First journal entry.'
    },
    {
        'entry_number': 2,
        'author': 'X',
        'title': 'Second Post',
        'date_written': 'January 1st, 2019',
        'content': 'Second journal entry.'
    }
]

@app.route('/')
def home():
    return render_template('home.html')


@app.route('/entryone')
def entry_one(entry):
    return render_template('journal_entry.html', post=entry)


@app.route('/entrytwo')
def entry_two():
    return render_template('journal_entry.html', entries=entries)


for post in entries:
    if post['entry_number'] == 1:
        entry_one(post)



if __name__ == '__main__':
    app.run()

What I am trying to do is send the data from my list of dictionaries to corresponding functions below. So the dictionary with entry_number 1 would have the data for the entryone function. And the dictionary with the entry number 2 would have the data for entrytwo and so on.

I try to do this and the code breaks. This is probably not the best way to do this to begin with, but I still need to fix it. Does anyone know the proper way to send the right dictionary data to the right function?

  • Check this link - https://stackoverflow.com/a/14032302/6917446. For example, you could add index of list parameter as part of url, and then handle its value there. – kosist Feb 28 '19 at 19:32
  • Huh, maybe I should try having dynamic url's instead of doing what I am doing. Thanks for showing my that. – Carter Weinberg Feb 28 '19 at 20:02

0 Answers0