# template using old content upon rerender

**URL:** <https://forum.djangoproject.com/t/template-using-old-content-upon-rerender/1297>\
**Category:** Using Django\
**Created:** [February 26, 2020, 5:45pm UTC](https://forum.djangoproject.com/t/template-using-old-content-upon-rerender/1297 "2020-02-26T17:45:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RyanRio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ryanrio/32/746_2.png) [@RyanRio](https://forum.djangoproject.com/u/RyanRio)\
**Post date:** [February 26, 2020, 5:45pm UTC](https://forum.djangoproject.com/t/template-using-old-content-upon-rerender/1297/1 "2020-02-26T17:45:31Z")

</div>

Hello,

I am trying to rerender my template with a different value on a POST request, the view is:

```python
class Playground(TemplateView):
    template = "test/playground.html"
    context = {}

    def get(self, request):
        self.context = dict(test="ok")
        return render(request, self.template, self.context)

    def post(self, request):
        type_of = request.POST.get('type')
        if type_of == "view_change":
            print(request.POST.get('to'))
            self.context = dict(test=request.POST.get('to'))
            return HttpResponseRedirect(request.build_absolute_uri(), json.dumps(self.context), content_type="application/json")

```

And then in the template I have: `<p>{{ test }}</p>`, however the value of test stays as `ok` (and I have confirmed the POST request is successful). Note that the same effect happens if I use render or HttpResponse. I understand I can just set the value of the `p` tag in the ajax response, however then I am unsure what to do with more complicated templates such as:

```auto
<ul>
    {% for choice in test %}
        <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
    {% endfor %}
</ul>

```

- How would I properly set the value of `test` here for instance?

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [February 26, 2020, 6:13pm UTC](https://forum.djangoproject.com/t/template-using-old-content-upon-rerender/1297/2 "2020-02-26T18:13:17Z")

</div>

Values for `self` will not be preserved across requests. And nor would you want them to be, since they’d be shared by visitors to your site.

It looks like you might want something like the messages framework to store something between requests? Or a database model?

---

<div class="post-metadata">

**Author:** ![RyanRio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ryanrio/32/746_2.png) [@RyanRio](https://forum.djangoproject.com/u/RyanRio)\
**Post date:** [February 26, 2020, 8:05pm UTC](https://forum.djangoproject.com/t/template-using-old-content-upon-rerender/1297/3 "2020-02-26T20:05:28Z")

</div>

Well, I’m not trying to preserve values for self, right? The only thing ‘saved’ is the context but I want to change that anyway (to the value of the POST). So even if I added a database model, which I probably should do in any case, the template is still not showing the correct visible value.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [February 26, 2020, 8:36pm UTC](https://forum.djangoproject.com/t/template-using-old-content-upon-rerender/1297/4 "2020-02-26T20:36:01Z")

</div>

I think you’re misunderstanding both Python’s class model and how Django uses class based views.

1. `self.context = ...` assigns to the instance only, not the class. If you instead mutated the dictionary, it would remain using the one on the class: `self.context["key"] = "value"`
2. Django instantiates a new `Playground` per response
3. Any class variables on `Playground` are shared amongst _all_ requests on the same process. So two users looking at the page at the same time could see each others’ data.
4. Conversely, they’re not shared between processes - which means when you deploy this, you might get an answer from server A with a redirect, then when you do the GET request it’s served by process B, where the modification to the class didn’t happen.

I suggest you try using function based views to begin with, they give you a clearer model of how Django answers a request.

---

<div class="post-metadata">

**Author:** ![RyanRio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ryanrio/32/746_2.png) [@RyanRio](https://forum.djangoproject.com/u/RyanRio)\
**Post date:** [February 27, 2020, 3:29pm UTC](https://forum.djangoproject.com/t/template-using-old-content-upon-rerender/1297/5 "2020-02-27T15:29:11Z")

</div>

Ah thanks for the tips, but I’m not trying to accomplish that, let me make it much simpler, sorry for the confusion! -

```python
class Playground(TemplateView):
    template = "test/playground.html"

    def get(self, request):
        context = dict(test="ok")
        return render(request, self.template, context)

    def post(self, request):
        type_of = request.POST.get('type')
        if type_of == "view_change":
            print(request.POST.get('to'))
            context = dict(test=request.POST.get('to'))
            return HttpResponseRedirect(request.build_absolute_uri(), json.dumps(context), content_type="application/json")

```

Ok, so as you see I’m not actually trying to save stuff to the class, that’s an entirely different issue, but am I still running into one of the issues you have mentioned?

Thanks!  
Ryan
