Thank you so much, I have fixed the problem, now I can redirect to the pages I want.
*morse_logs/game2.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 %} *morse_logs/views.html* @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')) On Wednesday, April 15, 2020 at 8:22:35 PM UTC+8, pui hei Li wrote: > > I am writing a view that retrieve an answer from game2.html, then check > the answer; if the answer is correct, the view will redirect user to > correct.html, if the answer is incorrect, then user will be redirected to > incorrect.html. > > The problem now is after clicking the submit button, user won't be > redirected. And after clicking the submit button, the url changed from > localhost:8000/game2 to > http://localhost:8000/game2/?ans2=4&game2Answer=Submit > > It seems the view is not verifying the answer, and it is also redirecting > user to anywhere. > > How do I solve it? > > *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(reverse('morse_logs:incorrect')) > else: > # user's score declared in model has no point > # display incorrect and 0 point added to user > return redirect(reverse('morse_logs:incorrect')) > > > ans2 = request.GET.get('ans2', '') > if ans2 == '': > ans2 = 0 > > verifyGame2(int(ans2)) > > return render(request, 'morse_logs/game2.html') > > > *morse_logs/game2.html* > > {% extends "morse_logs/base.html" %} > > {% block content %} > <title>GAME 2</title> > <div> > <h1>GAME 2</h1> > <h2>2 + 2 = ?</h2> > <form method="get" > > <input type="number" id="ans2" name="ans2"/><br><br> > <input type="submit" name="game2Answer"/> > </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'correct/', views.correct, name='correct'), > path(r'incorrect/', views.incorrect, name='incorrect'), > path(r'game1/', views.game1, name='game1'), > path(r'game2/', views.game2, name='game2'), > > ] > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/64d9a376-ac00-4f2d-858d-04c63e8826d6%40googlegroups.com.