我正在编写一个从 game2.html 检索答案的视图,检查答案;如果答案正确,视图将重定向用户到 Correct.html,如果答案不正确,则用户将重定向到 Correct.html。
现在的问题是点击提交按钮后,用户不会被重定向。但分数已更新。
单击“提交”按钮后,网址将从http://localhost:8000/game2/更改为http://localhost:8000/game2/?ans2=4&game2Answer=Submit,而不是重定向到 Correct.html 或 Correct.html 或 Correct.html
我想这可能是我的提交按钮没有触发重定向功能的问题,或者是我在视图中编写重定向函数的方式的问题,因为如果答案正确,分数实际上会更新。
那么,我该如何修复它,使其能够在进入 if-else 语句后重定向到 Correct.html 或 Correct.html 或不正确的.html。
morse_logs/views.py
@login_required()
def game2(request):
"""The Game2 page"""
if request.user and not request.user.is_anonymous:
user = request.user
def verifyGame2(val1):
user_score, created = userScore.objects.get_or_create(user=user)
if val1 == 4:
# user's score declared in model increase 5points
# display correct and 5 points added to user
user_score.score += 5
user_score.save()
return redirect('morse_logs:incorrect')
else:
# user's score declared in model has no point
# display incorrect and 0 point added to user
return redirect('morse_logs:incorrect')
ans2 = request.GET.get('ans2', '')
if ans2 == '':
ans2 = 0
verifyGame2(int(ans2))
return render(request, 'morse_logs/game2.html')
游戏2.html
{% extends "morse_logs/base.html" %}
{% block content %}
<title>GAME 2</title>
<div>
<h1>GAME 2</h1>
<h2>2 + 2 = ?</h2>
<form action="" method="post" >
<input type="number" id="ans1" name="ans1"/><br><br>
<input type="submit" name="game1Answer"/>
</form>
</div>
{% endblock content %}
morse_logs/ Correct.html
{% extends "morse_logs/base.html" %}
{% block content %}
<title>Correct!</title>
<div>
<h1>Congratulations! Your answer is CORRECT!</h1>
</div>
{% endblock content %}
morse_logs/in Correct.html
{% extends "morse_logs/base.html" %}
{% block content %}
<title>Inorrect...</title>
<div>
<h1>Unfortunately! Your answer is Incorrect!</h1>
</div>
函数式编程
宝慕林4294392
相关分类