lasordance
lasordance

Reputation: 49

Not getting the search result

I just start learning Flask and i'm trying to use a simple search form on my news headline site but when i submit the search it seems that it's not working, i'm using a simple flask get request Here is my code:

import feedparser
from flask import Flask
from flask import render_template
from flask import request

app = Flask(__name__)
rss_feed = {'wsj': "https://feeds.a.dj.com/rss/RSSWSJD.xml",
            'cnbc': "https://www.cnbc.com/id/100003114/device/rss/rss.html",
            'washington_post': "http://feeds.washingtonpost.com/rss/politics?itid=lk_inline_manual_2"}

@app.route("/")
def get_news():
    query = request.args.get('publication')
    if not query or query.lower not in rss_feed:
        publication = 'wsj'
    else:
        publication = query.lower

    feed = feedparser.parse(rss_feed[publication])
    return render_template("home.html", articles=feed['entries'])

if __name__ == "__main__":
    app.run(port=5000, debug=True)

and my html code:

<html>
    <head>
        <title>Headlines</title>
    </head>
    <body>
            <h1>Headlines</h1>
            <form>
          <input type="text" name="publication" placeholder="search">
          <input type="submit" value="Submit">
        </form>
            {% for article in articles %}
            <b><a href="{{article.link}}">{{article.title}}</a></b><br />
            <i>{{ article.published }}</i><br />
            <p>{{ article.summary }}</p>
    <hr />
    {% endfor %}
        </body>
</html>

Now when i submit a search for 'cnbc' it still display the result for 'wsj' what wrong with my code?

Upvotes: 0

Views: 73

Answers (1)

Federico Ba&#249;
Federico Ba&#249;

Reputation: 7675

I add 4 different notes in the code as comment, please have a look in the Glossary. Here is the solution:

Python file, here is where the error occur, long stoyry short your if statement was always returning True, hence the publication variable was set as 'wsj

import feedparser
from flask import render_template, request, Flask
# Note 1

app = Flask(__name__)
rss_feed = {'wsj': "https://feeds.a.dj.com/rss/RSSWSJD.xml",
            'cnbc': "https://www.cnbc.com/id/100003114/device/rss/rss.html",
            'washington_post': "http://feeds.washingtonpost.com/rss/politics?itid=lk_inline_manual_2"}


@app.route("/")
def get_news():
    
    
    if request.args.get('publication'):
        query = request.args.get('publication').lower() # Note 2
        print(f'Query is : {query}') # Note 4
        if not query or query not in rss_feed: # Note 3
            print('Publication is wsj') # Note 4
            publication = 'wsj'
        else:
            print('Publication is the query entry') # Note 4
            publication = query

        feed = feedparser.parse(rss_feed[publication])

        return render_template("view.html", articles=feed['entries'])
    return render_template("view.html")


if __name__ == "__main__":
    app.run(port=5000, debug=True)

HTML File

<html>
    <head>
        <title>Headlines</title>
    </head>
    <body>
        <h1>Headlines</h1>
        <!-- Note 5 -->
        <form method="GET"> 
          <input type="text" name="publication" placeholder="search">
          <input type="submit" value="Submit">
        </form>
            {% for article in articles %}
  
            <b><a href="{{article.link}}">{{article.title}}</a></b><br />
            <i>{{ article.published }}</i><br />
            <p>{{ article.summary }}</p>
            <hr />
            {% endfor %}
    </body>
</html>

NOTES GLOSSARY

  • Note 1: Have the imports from the same package in the same line

  • Note 2: Rather then check the query if is lower is better to turn it lower already in the variable as I did.

  • Note 3: Here is where the real issue was. This statement was always True hence setting the 'publication' variable as 'wsj'. This is because of add 'query.lower' not in rss_feed which obsiously it wasn't. And by the way you should have done it 'query.lower()'

  • Note 4: Just a reminder to How to find and troubleshot your code.The print() function is the first thing to learn in python and is the most usefull tool for this matter. This is how I found the error in less then 5 minutes because I let the code 'Speak' through the terminal.

  • Note 5: Few suggestion here, first add a method='GET' (not neccessary) just for readability. Then indent better the HTML file as it was all messed around.

Another solution that may be slightly clearer is this:

@app.route("/")
def get_news():
    
    if request.args.get('publication'):
        query = request.args.get('publication').lower() 
        if not query or query not in rss_feed: 
            publication = 'wsj'
            feed = feedparser.parse(rss_feed[publication])
            return render_template("view.html", articles=feed['entries'])
        
        publication = query
        feed = feedparser.parse(rss_feed[publication])
        return render_template("view.html", articles=feed['entries'])
    return render_template("view.html")

Upvotes: 1

Related Questions