Flask App will not load app.py (The file/path provided (app) does not appear to exist)

2024/7/6 21:46:38

My flask app is outputting 'no content' for the for() block and i dont know why.

I tested my query in app.py , here is app.py:

# mysql config
app.config['MYSQL_DATABASE_USER'] = 'user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'mypass'
app.config['MYSQL_DATABASE_DB'] = 'mydbname'
app.config['MYSQL_DATABASE_HOST'] = 'xxx.xxx.xxx.xxx'
mysql = MySQL()
mysql.init_app(app)
c = mysql.connect().cursor()
blogposts = c.execute("SELECT post_title, post_name FROM mydbname.wp_posts WHERE post_status='publish' ORDER BY RAND() LIMIT 3")@app.route('/', methods=('GET', 'POST'))
def email():form = EmailForm()if request.method == 'POST':if form.validate() == False:return 'Please fill in all fields <p><a href="/">Try Again</a></p>'else:msg = Message("Message from your visitor",sender='[email protected]',recipients=['[email protected]'])msg.body = """From: %s""" % (form.email.data)mail.send(msg)#return "Successfully  sent message!"return render_template('email_submit_thankyou.html')elif request.method == 'GET':return render_template('index.html', blogposts)

This is what my app.py looks like.

Below is my index.html in templates/:

    <ul>{% for blogpost in blogposts %}<li><a href="{{blogpost[1]}}">{{blogpost[0]}}</a></li>{% else %}<li>no content...</li>{% endfor %}<div class="clearL"> </div></ul>

I checked the query, and it returns desired output like:

ID   post_title       post_name
1    a title here     a-title-here
2    a title here2     a-title-here2
3    a title here3     a-title-here3

If i try to restart the dev server by running export FLASK APP=app.py', thenflask run`, i get an error of:

Error: The file/path provided (app) does not appear to exist.  Please verify the path is correct.  If app is not on PYTHONPATH, ensure the extension is .py

I've also tried running via export FLASK_APP=app.py then python -m flask run - this also gives the same error.

Thoughts on how to resolve?

Answer

You haven't got anything in your template called blogposts. You need to use keyword arguments to pass the data:

return render_template('index.html', blogposts=blogposts)

Also note you should really do that query inside the function, otherwise it will only ever execute on process start and you'll always have the same three posts.

https://en.xdnf.cn/q/119845.html

Related Q&A

how to create from month Gtk.Calendar a week calendar and display the notes in each day in python

I have created a calendar app with month and week view in python. In month view, I can write notes in each day, store them in a dictionary and save the dictionary in to disk so I can read it any time.…

How to access inner attribute class from outer class?

As title. the class set a attribute value inside inner class. then, access that inner attribute class from outer function. In below, attribute sets with inner function set_error. then, use outer functi…

Summing up the total based on the random number of inputs of a column

I need to sum up the "value" column amount for each value of col1 of the File1 and export it to an output file. Im new in python and need to do it for thousands of records.File1col1 col2 …

What is wrong with this Binomial Tree Backwards Induction European Call Option Pricing Function?

The function below works perfectly and only needs one thing: Removal of the for loop that creates the 1000 element array arr. Can you help me get rid of that for loop? Code is below #Test with europea…

Regex behaving weird when finding floating point strings [duplicate]

This question already has answers here:re.findall behaves weird(3 answers)Closed 4 years ago.So doing this (in python 3.7.3):>>> from re import findall >>> s = 7.95 + 10 pieces >&g…

how do i get this code to tell me what position a word in the sentence is

varSentence = ("The fat cat sat on the mat")print (varSentence)varWord = input("Enter word ")varSplit = varSentence.split()if varWord in varSplit:print ("Found word") else…

Reverse geocoding with Python/GoogleMaps API: How to parse response

Im attempting to use the Google Maps Geocoding API to find the State associated with a latitude and longitude with a Python script. I have no trouble getting back a response from the server, but when …

How to constantly generate random numbers inside for i in range loop

import random world_size=8 for i in range(world_size)chunk_type=random.randint(1,2)print(chunk_type)import randomclass chunk_type():chunk_type=random.randint(1,2)world_size=8for i in range(world_size):…

How to count IDs that have a given combination of flags?

I have dataframe like that. I need to choose and count all distinct users, who have title "banner_click" and "order". So I dont understand how to do it in pandas, in SQL you do like…

How to calculate quarterly wise churn and retention rate using python

How to calculate quarterly wise churn and retention rate with date column using python. with date column i want to group that quarterly using python.This is used to calculate the churn count groupby qu…