# Saving Session Value to Form Field

**URL:** <https://forum.djangoproject.com/t/saving-session-value-to-form-field/22003>\
**Category:** Forms & APIs\
**Created:** [July 1, 2023, 6:49pm UTC](https://forum.djangoproject.com/t/saving-session-value-to-form-field/22003 "2023-07-01T18:49:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![t298261](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/t298261/32/13833_2.png) [@t298261](https://forum.djangoproject.com/u/t298261)\
**Post date:** [July 1, 2023, 6:49pm UTC](https://forum.djangoproject.com/t/saving-session-value-to-form-field/22003/1 "2023-07-01T18:49:03Z")

</div>

Hi, I successfully have stored a session key and value called "viewed.

I am trying to get this session’s values in a separate function called “recently\_viewed” and store the session values in a field on a Form called RecentlyViewedForm. But the session values are not being saved. Any ideas?

```auto
def recently_viewed(request):

      if 'viewed' in request.session:
            id = request.session['viewed']. # This is working,.
      if request.method == 'POST':
            view = RecentlyViewedForm(request.POST)
            if view.is_valid():
                  view.instance.user_id = id
                  view.save() # This is not working. It's not being saved here.

def customerDashboard(request, id):
      user = User.objects.get(id=id)
      view = RecentlyViewedForm
      request.session['viewed'] = id
      context = {
            'user': user,
            'view': view,
      }
      return render(request,'employees/customer_dashboard.html', context)

.html snippet that is applicable:
<form action="recently-viewed" method='post' >
    {%csrf_token%}
    {{view}}
    </form>

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 1, 2023, 7:14pm UTC](https://forum.djangoproject.com/t/saving-session-value-to-form-field/22003/2 "2023-07-01T19:14:09Z")

</div>

What is `RecentlyViewedForm`?

If it’s a ModelForm, see the information about `commit=False` in the docs at [Creating forms from models | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/forms/modelforms/#the-save-method) for the appropriate method of updating an object being saved by a form.

---

<div class="post-metadata">

**Author:** ![t298261](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/t298261/32/13833_2.png) [@t298261](https://forum.djangoproject.com/u/t298261)\
**Post date:** [July 1, 2023, 7:23pm UTC](https://forum.djangoproject.com/t/saving-session-value-to-form-field/22003/3 "2023-07-01T19:23:16Z")

</div>

recently-reviewed activates the recently\_viewed function through URLs.py:

```
path('recently_viewed', views.recently_viewed, name='recently-viewed')

```

I don’t THINK I can use commit=False because nothing is being typed in the form. It’s just serving to activate the function. So I want to use sessions instead and have that fill in the form’s user\_id field with the id from the session.

---

<div class="post-metadata">

**Author:** ![t298261](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/t298261/32/13833_2.png) [@t298261](https://forum.djangoproject.com/u/t298261)\
**Post date:** [July 1, 2023, 7:45pm UTC](https://forum.djangoproject.com/t/saving-session-value-to-form-field/22003/4 "2023-07-01T19:45:30Z")

</div>

I tried adding commit=False here:

```auto
def recently_viewed(request):
      if 'viewed' in request.session:
            id = request.session['viewed']
      if request.method == 'POST':
            view = RecentlyViewedForm(request.POST)
            if view.is_valid():
                  update = view.save(commit=False )
                  view.instance.user_id = update.id
                  view.save()

#and 

def recently_viewed(request):
      if 'viewed' in request.session:
            id = request.session['viewed']
      if request.method == 'POST':
            view = RecentlyViewedForm(request.POST)
            if view.is_valid():
                  update = view.save(commit=False )
                  view.user_id = update.id
                  view.save()

```

To no avail.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 1, 2023, 11:06pm UTC](https://forum.djangoproject.com/t/saving-session-value-to-form-field/22003/5 "2023-07-01T23:06:34Z")

</div>

> [@t298261](#):
>
> I don’t THINK I can use commit=False because nothing is being typed in the form.

You’re submitting data, right? It doesn’t matter whether someone physically typed anything in the form. If you’re submitting form data (hence the reference to request.POST), then saving that form is saving data - and therefore can be used with `commit=False`.

(If there’s no data being submitted, then you’ve got something else fundamentally wrong in your approach, and should take a big step back and re-think this from the beginning.)

Regarding your two attempts, you’re modifying the wrong object after the save.

The return value from the call to save _is_ the instance of the object being saved. It’s _that_ object you want to modify, not the form.

---

<div class="post-metadata">

**Author:** ![t298261](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/t298261/32/13833_2.png) [@t298261](https://forum.djangoproject.com/u/t298261)\
**Post date:** [July 7, 2023, 6:42pm UTC](https://forum.djangoproject.com/t/saving-session-value-to-form-field/22003/6 "2023-07-07T18:42:52Z")

</div>

Solved. If anyone needs answer, let me know and I will post it.
