django adding element to list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

django adding element to list

rssail


I'm confused with how django adds elements to a list. consider the following:

    def add(request): 
        if request.method == "POST": 
        form = NewTaskForm(request.POST) 
            if form.is_valid(): 
                task = form.cleaned_data["task"]

                 request.session['tasks'].append(task) 
                # request.session['tasks'] += [task] 

                return HttpResponseRedirect(reverse("tasks:index"))
             else: 
                return render(request, "tasks/add.html",{
                      "form": form
                 }) 
        return render(request, "tasks/add.html",{ 
            "form": NewTaskForm() 
       })

if we add a print statement after request.session['tasks'].append(task) we get a list:

['check email']

we also get the same list if we comment the append line and use the correct way with +=

However, on the redirect to task/index the first way shows an empty list and the second way shows the list that's expected. Why? Whats going on?

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a603090f-676e-4608-83f5-ed3f85995dd0n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: django adding element to list

Joel Goldstick-2
On Wed, Nov 11, 2020 at 7:48 PM rssail <[hidden email]> wrote:
>
>
> I'm confused with how django adds elements to a list. consider the following:
>
>     def add(request):
>         if request.method == "POST":
>         form = NewTaskForm(request.POST)
should the above line be indented?

>             if form.is_valid():
>                 task = form.cleaned_data["task"]
>
>                  request.session['tasks'].append(task)
>                 # request.session['tasks'] += [task]
>
>                 return HttpResponseRedirect(reverse("tasks:index"))
>              else:
>                 return render(request, "tasks/add.html",{
>                       "form": form
>                  })
>         return render(request, "tasks/add.html",{
>             "form": NewTaskForm()
>        })
>
> if we add a print statement after request.session['tasks'].append(task) we get a list:
>
> ['check email']
>
> we also get the same list if we comment the append line and use the correct way with +=
>
> However, on the redirect to task/index the first way shows an empty list and the second way shows the list that's expected. Why? Whats going on?
>
> --
> 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 [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a603090f-676e-4608-83f5-ed3f85995dd0n%40googlegroups.com.



--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPM-O%2Bwp4UtJOeFMkvnugpnymUJJap_Twi8qd%2Be5JmduM0W6Gw%40mail.gmail.com.