Why the next parameter does NOT show up in request object?

122 views
Skip to first unread message

Christian Seberino

unread,
Nov 1, 2019, 2:09:01 PM11/1/19
to Django users
I can see the next parameter hanging off the URL of my log_in page.
However, I cannot *read* it in my view code.  The next parameter does NOT get 
passed to my Python code...Why not?  Is my form action = "." the problem below?

Here is the traceback.....


Here is my log in page...


def log_in(request):
        if request.method == "POST":
                form = pollster.forms.LogInForm(request.POST)
                if form.is_valid():
                        username = form.cleaned_data["username"].lower()
                        password = form.cleaned_data["password"]
                        next     = request.GET.get("next")
                        reply    = django.shortcuts.redirect(next)
                        user     = AUTH(username = username,
                                        password = password)
                        if user:
                                django.contrib.auth.login(request, user)
                else:
                        reply = django.shortcuts.render(request,
                                                        "log_in.html",
                                                        {"form" : form})
        else:
                form  = pollster.forms.LogInForm()
                reply = django.shortcuts.render(request,
                                                "log_in.html",
                                                {"form" : form})

        return reply

Here is my template...

<!DOCTYPE html>
<html>
<head>
        <title>Pollster</title>
        <link type = "text/css"
              href = "/static/base.css"
              rel  = "stylesheet">
        <meta content    = "text/html; charset=utf-8"
              http-equiv = "content-type">
</head>
<body>

<div id = "log_in">
        <form action = "." method = "post">
                <p>Username:</p>

                <p>{{form.username}}</p>

                <p>Password:</p>

                <p>{{form.password}}</p>

                <p><input type = "submit" value = "Submit"/></p>

                {% csrf_token %}
        </form>
</div>

</body>
</html>



Andréas Kühne

unread,
Nov 1, 2019, 4:43:26 PM11/1/19
to django...@googlegroups.com
Hi,

There are 2 errors in your code. 

First, when you do a request to the page, it is a get (you go from another page to this page). In this case the next parameter is there and you receive it in the "else" part of your view function (because the request is a get). So you have the next parameter as a get parameter in that case.
Second, when you submit the form, it is submitted via a post. In this case the get parameters are filled out, but the POST part of the request is. So you populate your form with the information sent via a POST, and you do this here:
form = pollster.forms.LogInForm(request.POST)

So the GET part of the request is empty in this case, because the request is a POST.

What you need to do is get the parameter from the request when you populate the form and then send it to the template:
def log_in(request):
        if request.method == "POST":
                form = pollster.forms.LogInForm(request.POST)
                if form.is_valid():
                        username = form.cleaned_data["username"].lower()
                        password = form.cleaned_data["password"]
                        next     = form.cleaned_data('next')
                        reply    = django.shortcuts.redirect(next)
                        user     = AUTH(username = username,
                                        password = password)
                        if user:
                                django.contrib.auth.login(request, user)
                else:
                        reply = django.shortcuts.render(request,
                                                        "log_in.html",
                                                        {"form" : form})
        else:
                form  = pollster.forms.LogInForm(initial_data={'next': request.GET.get('next')})
                reply = django.shortcuts.render(request,
                                                "log_in.html",
                                                {"form" : form})

        return reply

So I have updated the code both for the get and post. The get populates the initial data for the form (the next parameter). You of course need to add the next field to your form code.

Then in the template you can add the following:

<div id = "log_in">
        <form action = "." method = "post">
                <input type="hidden" name="next" value="{{form.next.value}}">
                <p>Username:</p>

                <p>{{form.username}}</p>

                <p>Password:</p>

                <p>{{form.password}}</p>

                <p><input type = "submit" value = "Submit"/></p>

                {% csrf_token %}
        </form>
</div>

This way your form will include the posted next data and you can get the next field from the login form.

Regards,

Andréas


--
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/bed8ec66-572d-4306-a496-edf9b69142bb%40googlegroups.com.

Christian Seberino

unread,
Nov 1, 2019, 6:07:36 PM11/1/19
to django...@googlegroups.com
Andreas

Thanks so much.  This was very helpful.  For what its worth, the keyword on the Form class
seems to be "initial" rather than "initial_data" now.  Thanks again!  Awesome stuff!

cs
Reply all
Reply to author
Forward
0 new messages