So the "return" inside the inner function won't automatically "trigger". 
`redirect` is a shortcut function which constructs a HttpResponseRedirect. 
At the moment, you're calling the function, and the HttpResponseRedirect 
instance is returned from the function you're not actually doing anything 
with it, you're throwing it away. You'd need to either `return verifyGame2` 
or save the response `redirect_instance = verifyGame2` and then `return 
redirect_instance` 

But your flow in this function seems very odd. Your actions are exactly the 
same regardless if it's a GET or POST. Your form's submit should be POST 
and not a GET (since you're sending data). In that case; your code should 
be looking at the request's method, if it's a GET, then render the 
game2.html, if it's a POST, then you need fetch the POST data (the answer) 
and do the processing as you've done.

It's quite unnecessary to have an inner function in this situation.

Quite a lot to improve on with this FBV.

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/0e1cf17c-a5cf-4e58-b3de-9e243ee8ed3b%40googlegroups.com.

Reply via email to