Flask函数将对象返回到另一个函数

时间:2018-03-12 17:28:38

标签: python function flask

烧瓶新手,试图了解如何在函数之间传递函数参数。我需要知道如何才能做到这一点:

@home.route('/admin/dashboard')
@login_required
def admin_dashboard():
    if not current_user.is_admin:
        abort(403)

    td = timedelta
    t = date.today()
    ssun = t + td(days=-1 - t.weekday())
    mmon = t + td(days=0 - t.weekday())
    ttue = t + td(days=1 - t.weekday())
    wwed = t + td(days=2 - t.weekday())
    tthu = t + td(days=3 - t.weekday())
    ffri = t + td(days=4 - t.weekday())
    ssat = t + td(days=5 - t.weekday())
    today = t.strftime('%#m-%#d-%Y')
    sun = ssun.strftime('%#m-%#d')
    mon = mmon.strftime('%#m-%#d')
    tue = ttue.strftime('%#m-%#d')
    wed = wwed.strftime('%#m-%#d')
    thu = tthu.strftime('%#m-%#d')
    fri = ffri.strftime('%#m-%#d')
    sat = ssat.strftime('%#m-%#d')

    return render_template('home/admin_dashboard.html', today=today, 
                        sun=sun, mon=mon, tue=tue, wed=wed, thu=thu,
                        fri=fri, sat=sat, title="Dashboard-Admin")

哪个工作完美! 还有更多内容:

def get_dates():
    td = timedelta
    t = date.today()
    ssun = t + td(days=-1 - t.weekday())
    mmon = t + td(days=0 - t.weekday())
    ttue = t + td(days=1 - t.weekday())
    wwed = t + td(days=2 - t.weekday())
    tthu = t + td(days=3 - t.weekday())
    ffri = t + td(days=4 - t.weekday())
    ssat = t + td(days=5 - t.weekday())
    today = t.strftime('%#m-%#d-%Y')
    sun = ssun.strftime('%#m-%#d')
    mon = mmon.strftime('%#m-%#d')
    tue = ttue.strftime('%#m-%#d')
    wed = wwed.strftime('%#m-%#d')
    thu = tthu.strftime('%#m-%#d')
    fri = ffri.strftime('%#m-%#d')
    sat = ssat.strftime('%#m-%#d')

    return (today, sun, mon, tue, wed, thu, fri, sat)

@home.route('/admin/dashboard')
@login_required
def admin_dashboard():
    if not current_user.is_admin:
        abort(403)

    get_dates()

    return render_template('home/admin_dashboard.html', today=today, 
                        sun=sun, mon=mon, tue=tue, wed=wed, thu=thu,
                        fri=fri, sat=sat, title="Dashboard-Admin")

哪个甚至没有接近工作!

3 个答案:

答案 0 :(得分:0)

在您的' admin_dashboard'中尝试此操作功能:

ESCAPECHAR '@'

答案 1 :(得分:0)

如果要在today函数中使用变量sunmonadmin_dashboard(),...,则需要在{{1}中定义它们功能。

admin_dashboard()

答案 2 :(得分:0)

get_dates()中定义的变量范围是该函数的本地变量。 除非您在下一个函数中再次明确地定义它们,否则不能"在这个函数之外使用" 这些变量。

from datetime import *

def get_dates():
    td = timedelta
    t = date.today()
    ssun = t + td(days=-1 - t.weekday())
    mmon = t + td(days=0 - t.weekday())
    ttue = t + td(days=1 - t.weekday())
    wwed = t + td(days=2 - t.weekday())
    tthu = t + td(days=3 - t.weekday())
    ffri = t + td(days=4 - t.weekday())
    ssat = t + td(days=5 - t.weekday())
    today = t.strftime('%#m-%#d-%Y')
    sun = ssun.strftime('%#m-%#d')
    mon = mmon.strftime('%#m-%#d')
    tue = ttue.strftime('%#m-%#d')
    wed = wwed.strftime('%#m-%#d')
    thu = tthu.strftime('%#m-%#d')
    fri = ffri.strftime('%#m-%#d')
    sat = ssat.strftime('%#m-%#d')

    return (today, sun, mon, tue, wed, thu, fri, sat)

def admin_dashboard():
    (today, sun, mon, tue, wed, thu, fri, sat) = get_dates()
    return today  

admin_dashboard()  #3-13-2018