单击提交按钮后无法重定向到其他页面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单击提交按钮后无法重定向到其他页面相关的知识,希望对你有一定的参考价值。
我正在编写一个从game2.html检索答案的视图,检查答案;如果答案正确,则视图将用户重定向到correct.html;如果答案不正确,则用户将重定向至correct.html。
现在的问题是,单击提交按钮后,将不会重定向用户。但是分数已更新。
我单击提交按钮后,URL将从http://localhost:8000/game2/更改为http://localhost:8000/game2/?ans2=4&game2Answer=Submit,而不是重定向到Correct.html或correct..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')
game2.html
% extends "morse_logs/base.html" %
% block content %
<title>GAME 2</title>
<div>
<h1>GAME 2</h1>
<h2>2 + 2 = ?</h2>
<form action="" method="get" >
<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 / incorrect.html
% extends "morse_logs/base.html" %
% block content %
<title>Inorrect...</title>
<div>
<h1>Unfortunately! Your answer is Incorrect!</h1>
</div>
% endblock content %
morse_logs / urls.py
from django.urls import path, include
from morse_logs import views
app_name = 'morse_logs'
urlpatterns = [
#The path() function is passed four arguments, two required: route and view, and two optional: kwargs, and name.
# Home Page
path(r'', views.index, name='index'),
# Page that shows all topics
path(r'topics/', views.topics, name='topics'),
path(r'cipher/', views.cipher, name='cipher'),
path(r'decipher/', views.decipher, name='decipher'),
path(r'tutorialIndex/', views.tutorialIndex, name='tutorialIndex'),
path(r'gameDirectory/', views.gameDirectory, name='gameDirectory'),
path(r'game1/', views.game1, name='game1'),
path(r'game2/', views.game2, name='game2'),
path(r'correct/', views.correct, name='correct'),
path(r'incorrect/', views.incorrect, name='incorrect'),
]
答案
您应该更改您的
以上是关于单击提交按钮后无法重定向到其他页面的主要内容,如果未能解决你的问题,请参考以下文章
在 ASP.net MVC 5 应用程序中单击按钮时使用 JavaScript/jQuery 将用户重定向到页面
通过 Jquery Form Validator 单击提交后如何重定向页面