Cannot Redirect to other page

46 views
Skip to first unread message

pui hei Li

unread,
Apr 15, 2020, 8:22:35 AM4/15/20
to Django users
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'),

]

Gavin Wiener

unread,
Apr 16, 2020, 8:01:08 AM4/16/20
to Django users
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.

pui hei Li

unread,
Apr 16, 2020, 12:24:09 PM4/16/20
to django...@googlegroups.com
Thank you for your reply, but I don't really understand how am i able to make it to the page I want, does it mean I need to write a new function in view and pass in the retrieved answer from user, then do the redirection/ rendering? Could you give me some hints about how the syntax would be, because I am a total newbie in Django and couldn't find much resources about my situation. Thank you!

Gavin Wiener <gavin...@gmail.com> 於 2020年4月16日週四 下午8:00寫道:
--
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...@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.
Message has been deleted

Gavin Wiener

unread,
Apr 16, 2020, 8:51:38 PM4/16/20
to Django users
I did include example, the last sentence of the first paragraph.

You need to get rid of the inner function, I don't know why that was written. I needs to look more

if request.method =="GET":
   
return the regular page
elif request.method == "POST":
   process the answers that have been sent
by the user

Basically follow this, https://docs.djangoproject.com/en/3.0/topics/forms/#get-and-post

You'll need to make changes to;

1. Your template
2. Removing the inner function
3. Processing the form correctly
4. Handling the GET and POST differently


On Wednesday, April 15, 2020 at 8:22:35 PM UTC+8, pui hei Li wrote:

pui hei Li

unread,
Apr 17, 2020, 4:59:22 AM4/17/20
to Django users
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:
Reply all
Reply to author
Forward
0 new messages