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

2019-11-01 Thread Christian Seberino
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:
>
> 
> 
>  value="{{form.next.value}}">
> Username:
>
> {{form.username}}
>
> Password:
>
> {{form.password}}
>
> 
>
> {% csrf_token %}
> 
> 
>
> 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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAG5-5iJ9hKQaWuQM2iF4pPzRuXntcNfYKonzAFqHmGUvAz6QCQ%40mail.gmail.com.


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

2019-11-01 Thread Andréas Kühne
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:




Username:

{{form.username}}

Password:

{{form.password}}



{% csrf_token %}



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

Regards,

Andréas


Den fre 1 nov. 2019 kl 19:10 skrev Christian Seberino :

> 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.
>
> https://dpaste.de/H7KU
>
> 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...
>
> 
> 
> 
> Pollster
>href = "/static/base.css"
>   rel  = "stylesheet">
>http-equiv = "content-type">
> 
> 
>
> 
> 
> Username:
>
> {{form.username}}
>
> Password:
>
> {{form.password}}
>
> 
>
> {% csrf_token %}
> 
> 
>
> 
> 
>
>
>
> --
> 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/bed8ec66-572d-4306-a496-edf9b69142bb%40googlegroups.com
> 

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

2019-11-01 Thread Christian Seberino
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.

https://dpaste.de/H7KU

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




Pollster







Username:

{{form.username}}

Password:

{{form.password}}



{% csrf_token %}








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