Bird
0
0

This Flask code raises an error when caching is applied:

medium📝 Debug Q7 of 15
Flask - Middleware and Extensions
This Flask code raises an error when caching is applied:
from flask import Flask
from flask_caching import Cache
app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'})

@app.route('/time')
@cache.cached(timeout=5)
def get_time():
    return time.ctime()

What is the likely cause of the error?
AWrong decorator usage order
BMissing import of the time module
CIncorrect cache timeout value
DFlask app not created properly
Step-by-Step Solution
Solution:
  1. Step 1: Check code for time usage

    Function uses time.ctime() but time module is not imported.
  2. Step 2: Effect of missing import

    Python raises NameError for undefined 'time'.
  3. Final Answer:

    Missing import of the time module -> Option B
  4. Quick Check:

    Use 'import time' when calling time functions [OK]
Quick Trick: Import modules before using their functions [OK]
Common Mistakes:
MISTAKES
  • Ignoring missing imports
  • Assuming cache timeout causes error
  • Thinking decorator order matters here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes