blosche
blosche

Reputation: 47

Force page refresh

I have a simple class based view that extends Django's (1.10) generic UpdateView. On the GET, the view presents a pre-populated form from the linked model. On the POST, the view updates the db record and then redirects the user to the list page that shows all the database records in a table. I am using the success_url property to do the redirect.

However, when the redirect finishes and the list page is displayed, the changes I made during the update are not reflected. When I look in the database, the changes are there.

The redirect must be pulling from a previous cache or something but how do I prevent this from happening? I am using Django 1.10; Ubuntu OS; Chrome Browser; supplied wsgi dev django webserver.

 class UpdateBrandAmbassador(UpdateView):

    model = BrandAmbassador
    form_class = BrandAmbassadorForm
    template_name = 'core/base_form.html'
    title = "Update Brand Ambassador"
    success_url = "/brand_ambassadors"


class BrandAmbassadorList(TemplateView):

    headers = [
        ("Name", 16),
        ("Address", 20),
        ("City, State", 10),
        ("Zip", 5),
        ("E-Mail", 15),
        ("Phone", 10),
    ]
    data = [each.as_list() for each in BrandAmbassador.objects.all()]
    template_name = 'core/list_objects.html'
    title = "Brand Ambassador Master"
    update_delete_url = "brand_ambassadors"

class BrandAmbassador(models.Model):

    name_last = CharField(max_length=255)
    name_first = CharField(max_length=255)
    address_1 = CharField(max_length=255, null=True)
    address_2 = CharField(max_length=255, null=True)
    city = CharField(max_length=255, null=True)
    state = CharField(max_length=255, null=True)
    zip_code = CharField(max_length=255, null=True)
    email = EmailField(null=True)
    phone = CharField(max_length=255, null=True)

    region = ForeignKey(Region, related_name="brand_ambassadors")

    def __str__(self):
        return " ".join([self.name_first, self.name_last])


    def as_list(self):

        return [
            self.id,
            ", ".join([self.name_last, self.name_first]),
            " ".join([self.address_1, self.address_2]),
            ", ".join([self.city, self.state]),
            self.zip_code,
            self.email,
            self.phone
        ]

Upvotes: 1

Views: 1316

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599540

The problem is in your ListView. You must not do operations on data at class level, as they will be done only once at import time.

Usually if you need to do calculations you would do so in a method, for example get_context_data. But I don't see why you need to do that list comprehension at all; your queryset of BrandAmbassador objects will be sent to the template already by the view, so if you need to you can call as_list on them there when you iterate through.

{% for obj in object_list %}
    {{ obj.as_list }}
{% endfor %}

Upvotes: 1

Related Questions