Friday, November 1, 2019

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

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
 
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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG5-5iJ9hKQaWuQM2iF4pPzRuXntcNfYKonzAFqHmGUvAz6QCQ%40mail.gmail.com.

No comments:

Post a Comment