如何使用提交按钮重定向到其他页面

我正在编写一个从 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>


侃侃无极
浏览 137回答 2
2回答

函数式编程

首先,我将模板表单方法从“GET”更改为“POST”,并添加 {% csrf_token %}。其次,我将视图更改为两部分:第一部分是当用户第一次输入 game2.html(GET 请求)时,它将向用户呈现 game2.html。第二部分基本上是我之前所做的,但这次我添加了一个响应用户 POST 请求的案例,并从那里重定向到 Correct.html 或 Correct.html 或 Correct.html游戏2.html{% extends "morse_logs/base.html" %}{% block content %}&nbsp; &nbsp; <title>GAME 2</title><div>&nbsp; &nbsp; <h1>GAME 2</h1>&nbsp; &nbsp; <h2>2 + 2 = ?</h2>&nbsp; &nbsp; <form method="POST">&nbsp; &nbsp; &nbsp; &nbsp; {% csrf_token %}&nbsp; &nbsp; &nbsp; &nbsp; <input type="number" id="ans2" name="ans2"/><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="Submit"/>&nbsp; &nbsp; </form></div>{% endblock content %}views.py@login_required()def game2(request):&nbsp; &nbsp; """The Game2 page"""&nbsp; &nbsp; if request.method == "GET":&nbsp; &nbsp; &nbsp; &nbsp; return render(request, 'morse_logs/game2.html')&nbsp; &nbsp; elif request.method == "POST":&nbsp; &nbsp; &nbsp; &nbsp; if request.user and not request.user.is_anonymous:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user = request.user&nbsp; &nbsp; &nbsp; &nbsp; user_score, created = userScore.objects.get_or_create(user=user)&nbsp; &nbsp; &nbsp; &nbsp; ans2 = request.POST.get('ans2', '') #fetch the POST data from template&nbsp; &nbsp; &nbsp; &nbsp; if ans2 == '':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ans2 = 0&nbsp; &nbsp; &nbsp; &nbsp; ans2 = int(ans2)&nbsp; &nbsp; &nbsp; &nbsp; if ans2 == 4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # user's score declared in model increase 5points&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # display correct and 5 points added to user&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user_score.score += 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user_score.save()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect(reverse('morse_logs:correct'))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # user's score declared in model has no point&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # display incorrect and 0 point added to user&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect(reverse('morse_logs:incorrect'))

宝慕林4294392

你应该改变你的重定向('morse_logs:不正确的.html')到重定向('url_name')如果您使用 django 版本 >2.0,还请删除 app_name
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5