Flask新手求助:如何返回多个值?return与print行为差异问题
Hey there! Let's clear up the confusion between print and return first, then fix your code so you can show all those news titles on your Flask page.
The Core Difference: Print vs Return
Let's break this down simply:
print()sends text to your command line/console—it's just for you to see, and doesn't pass any data back to the code that called the function.return()sends data back to the caller (in your case, your Flask route function). But here's the key: as soon as a function hits areturnstatement, it stops running immediately. That's why your loop only returns the first news title—once it hitsreturn(row['title']), the function ends, and the rest of the loop never runs.
Fixing Your Code to Return Multiple Titles
You need to collect all the titles first, then return the whole collection. Here's how to do it step by step:
Step 1: Update my_algorithms.py to Collect All Titles
Instead of returning the first title right away, we'll gather all titles into a list, then return that list:
def receive_text_from_form(text): news_keyword = text newsapi = NewsApiClient(api_key='0fb13acc3bc8480eafedb87afa941f7e') data = newsapi.get_everything(q=news_keyword) jdict = data.get('articles', []) # Add a default empty list to avoid errors if no articles are found # Collect all titles into a list all_titles = [] for row in jdict: all_titles.append(row['title']) return all_titles # Return the full list of titles
Step 2: Display the Titles in Flask (Two Options)
Now that your function returns all titles, you can show them on your page in two common ways:
Option 1: Quick Fix with String Joining
If you want a simple solution without a new template, join the list into a string with line breaks (using <br> for HTML line breaks):
@app.route('/', methods=['POST']) def my_form_post(): text = request.form['text'] titles = my_algorithms.receive_text_from_form(text) # Join titles with HTML line breaks so they show up on separate lines return '<br>'.join(titles)
Option 2: Better Practice with a Template
For cleaner, more maintainable code (and better-looking pages), use a Flask template to render the titles as a list:
- First, update your Flask route to pass the titles to a template:
@app.route('/', methods=['POST']) def my_form_post(): text = request.form['text'] titles = my_algorithms.receive_text_from_form(text) # Pass both the search term and titles to the results template return render_template('results.html', search_term=text, titles=titles)
- Create a
results.htmltemplate in yourtemplatesfolder:
<!DOCTYPE html> <html> <head> <title>News Results</title> </head> <body> <h2>News for "{{ search_term }}"</h2> {% if titles %} <ul> {% for title in titles %} <li>{{ title }}</li> {% endfor %} </ul> {% else %} <p>No news found for that term.</p> {% endif %} <a href="/">Search again</a> </body> </html>
Final Notes
- Always remember:
returnexits the function immediately, so never use it inside a loop if you need to process all items. - Using templates is the standard Flask way to display dynamic content—it keeps your logic and presentation separate, making your code easier to debug and update.
内容的提问来源于stack exchange,提问作者Christopher

