When submitting data troughs user input and the data doesn't match anything in mongodb collections the Jinja template throw an error (TypeError: 'NoneType' object is not subscriptable) but when data is existing in the collection the data loads to the template without any problem and i don't know how to handle this error. And as well how i can refresh the page After the submit because the results from search input doesn't disappear after reloading the page. Any help will be appreciated and thanks in advance.
this is my Python code:
import os
from datetime import datetime
from flask import (
Flask, flash, render_template,
redirect, request, session, url_for)
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from werkzeug.security import generate_password_hash, check_password_hash
if os.path.exists("env.py"):
import env
app = Flask(__name__)
app.config["MONGO_DBNAME"] = os.environ.get("MONGO_DBNAME")
app.config["MONGO_URI"] = os.environ.get("MONGO_URI")
app.secret_key = os.environ.get("SECRET_KEY")
mongo = PyMongo(app)
@app.route("/")
@app.route("/home", methods=["GET", "POST"])
def home():
return render_template("home.html")
@app.route("/search", methods=["GET", "POST"])
def search():
query = request.form.get("company-name")
company = mongo.db.company.find_one(
{"company_name": query})["company_name"]
if company:
review = list(mongo.db.review.find({"company_name": company}))
else:
return redirect(url_for("home"))
return render_template("home.html", review=review)
html Jinja tempalte
<form method="POST" action="{{ url_for('search') }}">
<label name="company-name" for="company-
name">Company Name</label>
<input type="text" id="company-name" name="company-name">
<button type="submit"
name="action"><span>Search</span>
<i ></i><i ></i>
</button>
</form>
<div>
{% if review|length > 0 %}
{% for set in review %}
<strong>{{ set.company_name }}</strong> <br>
<strong>{{ set.username }}</strong><br>
<strong>{{ set.score }}</strong><br>
<strong>{{ set.review_content }}</strong><br>
{% endfor %}
{% else %}
<h3 >No Results Found</h3>
{% endif %}
</div>
CodePudding user response:
I found the solution, but still, I have no idea what was wrong with the code I wrote.
python code
@app.route("/search", methods=["GET", "POST"])
def search():
query = request.form.get("company-name")
review = list(mongo.db.review.find(
{"company_name": query}))
return render_template("home.html", query=query, review=review)
html :
<div id="review-search">
{% if review|length > 0 %}
<h2 >Results for <span > {{ query }} </span>
</h2>
<h3> total {{ total_score }}</h3>
{% for set in review %}
<div >
<div >
<div >
<div >
<h5>{{ set.username }}</h5>
</div>
</div>
<div >
<div >
<p>{{ set.score }}/10</p>
</div>
</div>
<div >
<div >
<p>{{ set.review_content }}</p>
</div>
</div>
</div>
</div>
{% endfor %}
{% else %}
<h2 >No Results Found for <span> "{{ query }}"</span>
</h2>
{% endif %}
</div>
CodePudding user response:
use
try:
// task1
except:
// executed when there's runtime error while doing task1