【发布时间】:2020-12-19 00:27:57
【问题描述】:
我正在使用烧瓶、python 和 sqlite 进行 cs50 最终项目。我的问题是了解如何从同一路由调用 2 个函数,或从另一个调用一个。或者坦率地说,只是如何实现 1 - 根据日期和月份分配 astro_sign 以及 2 - 将 astro_sign 更新为 3 的 sqlite db (signs.db) 的目标 - 显示在 index.html 上。我正在尝试使用此示例代码https://www.geeksforgeeks.org/program-display-astrological-sign-zodiac-sign-given-date-birth/,但无法使 zodiac 功能正常工作。我已经尝试在没有注册路由中的 if/elif 语句的功能的情况下执行此操作,但我得到“NameError:名称'astro_sign'未定义”或“UnboundLocalError:分配前引用的局部变量'astro_sign'”取决于尝试.我在这里找到了一个讨论局部变量与全局变量的示例,并建议添加“astro ='',但随后我的signs.db 被更新为一个空字符串,就好像函数永远不会运行一样。我还尝试使用@/zodiac 路由重定向,但无法正确处理。我找到了这个:How can we call one route from another route with parameters using python flask.? 但不明白。下面的当前尝试,我尝试将函数放入 helper.py,但这完全杀死了应用程序。我不能弄清楚如何返回函数,全局命名变量,我非常沮丧。我从 html 文档中提取月份和日期作为整数:
register.html:
<form action="/register" method="post">
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text">
</div>
<div class="form-group">
<select class="form-control" name="month">
<option value= 1>January</option>
<option value= 2>February</option>
<option value= 3>March</option>
<option value= 4>April</option>
<option value= 5>May</option>
<option value= 6>June</option>
<option value= 7>July</option>
<option value= 8>August</option>
<option value= 9>September</option>
<option value= 10>October</option>
<option value= 11>November</option>
<option value= 12>December</option>
</select>
</div>
<div class="form-group">
<input class="form-control" name="day" placeholder="Day of Birth" type="number">
</div>
<div class="form-group">
<input class="form-control" name="password" placeholder="Password" type="password">
</div>
<div class="form-group">
<input class="form-control" name="confirmation" placeholder="Confirm Password" type="password">
</div>
<button class="btn btn-primary" type="submit">Register</button>
</form>
然后在 application.py 中,一切正常,直到我尝试确定星座并发送到 db。
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
# if user reached route via POST
if request.method == "POST":
username = request.form.get("username")
month = request.form.get("month")
day = request.form.get("day")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
astro_sign = ''
#there are other checks in here that I removed to make this shorter
#insert new users
result = db.execute("INSERT INTO users (username, hash, month, day) VALUES (:username, :password, :month, :day)", username=username, password=hash, month=month, day=day)
# ensure username is unique
if not result:
return apology("username is already registered")
# remember which user has logged in
session["user_id"] = result
#try to call the function from helper.py and update astro_sign
if astro_sign is '':
zodiac_sign(day, month)
return astro_sign
#insert into signs.db that accepts astro_sign as text
db.execute("INSERT INTO signs (user_id, astro_sign) VALUES (:user_id, :astro_sign)",
user_id=session["user_id"], astro_sign=astro_sign)
#redirect to login
return render_template("login.html")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.html")
在 helper.py 中,我最近的尝试现在导致应用在注册后完全崩溃。
def zodiac_sign(day, month):
astro_sign = ''
# checks month and date within the valid range
# of a specified zodiac
if month is 12:
astro_sign = 'Sagittarius' if (day < 22) else 'capricorn'
elif month is 1:
astro_sign = 'Capricorn' if (day < 20) else 'aquarius'
elif month is 2:
astro_sign = 'Aquarius' if (day < 19) else 'pisces'
elif month is 3:
astro_sign = 'Pisces' if (day < 21) else 'aries'
#you get the point
elif month is 11:
astro_sign = 'scorpio' if (day < 22) else 'sagittarius'
return astro_sign
Any help would be so appreciated!
【问题讨论】:
-
我认为这个问题需要澄清一下:当您说您当前的代码“完全杀死应用程序”时,您看到的实际错误是什么?这真的可以帮助我们了解出了什么问题。此外,缩进看起来有点不对劲:我假设
zodiac_sign函数的内容是缩进的,但如果不是,那肯定会导致问题。您是从application.py中的helper模块导入zodiac_sign函数吗? -
对不起。我很沮丧。在尝试注册新用户后,我当前使用 helper.py 中的函数的代码会导致屏幕完全变白(无法恢复)。在任何情况下,我都无法让 astro_sign 在signs.db 中更新。我确实忘了说我正在将 zodiac 函数导入到 application.py 中。我是。谢谢你至少回复。我是新来的堆栈和尝试。
-
我正在使用 cs50 IDE。我没有收到错误,但我想我试图从 helpers.py 中的 zodiac 函数重定向到路由索引。我尝试了很多东西,我可能复制了错误的版本。对不起。
-
如果是这样的话,在我的 helpers.py zodiac 函数的底部有返回重定向(“/”)。不管怎样,我需要帮助。我可以提供您需要的任何代码来查看正在导入的内容。我希望我不是虚拟学生,因为我想真正理解这一点。无论哪种方式,在 application.py 的顶部:“从助手导入道歉,login_required,zodiac_sign”。当我运行“烧瓶运行”时,注册后出现白屏。我一直认为这应该在注册路线中轻松完成,但我不知道如何。我对任何链接或更好的学习策略感兴趣。