亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用提交按鈕重定向到其他頁面

如何使用提交按鈕重定向到其他頁面

侃侃無極 2023-12-25 17:17:13
我正在編寫一個從 game2.html 檢索答案的視圖,檢查答案;如果答案正確,視圖將重定向用戶到 Correct.html,如果答案不正確,則用戶將重定向到 Correct.html?,F在的問題是點擊提交按鈕后,用戶不會被重定向。但分數已更新。單擊“提交”按鈕后,網址將從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>
查看完整描述

2 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

首先,我將模板表單方法從“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 %}

    <title>GAME 2</title>

<div>

    <h1>GAME 2</h1>

    <h2>2 + 2 = ?</h2>

    <form method="POST">

        {% csrf_token %}

        <input type="number" id="ans2" name="ans2"/><br><br>

        <input type="submit" name="Submit"/>

    </form>

</div>

{% endblock content %}

views.py


@login_required()

def game2(request):

    """The Game2 page"""

    if request.method == "GET":

        return render(request, 'morse_logs/game2.html')

    elif request.method == "POST":

        if request.user and not request.user.is_anonymous:

            user = request.user


        user_score, created = userScore.objects.get_or_create(user=user)


        ans2 = request.POST.get('ans2', '') #fetch the POST data from template

        if ans2 == '':

            ans2 = 0


        ans2 = int(ans2)

        if ans2 == 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(reverse('morse_logs:correct'))

        else:

            # user's score declared in model has no point

            # display incorrect and 0 point added to user

            return redirect(reverse('morse_logs:incorrect'))


查看完整回答
反對 回復 2023-12-25
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

你應該改變你的

重定向('morse_logs:不正確的.html')

重定向('url_name')

如果您使用 django 版本 >2.0,還請刪除 app_name


查看完整回答
反對 回復 2023-12-25
  • 2 回答
  • 0 關注
  • 192 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號