trpt4him
trpt4him

Reputation: 1906

Django + uWSGI + nginx = strange caching?

I am using Django, uWSGI, and nginx together on my web application. I am having a weird issue where a DateField which prepopulates with today's date is getting stuck on a date prior to today. I've narrowed it down to the date that I last restarted the uWSGI daemon. If I restart the daemon and refresh the page, it's immediately fixed, but if I revisit the page tomorrow, it will have today's date.

I thought that neither uWSGI nor nginx had any caching enabled by default.

I won't bother including the nginx config because I don't believe that's related, but here's my uwsgi config:

[uwsgi]
chdir=/home/nick/django-prod/budget
pythonpath=..
socket=/home/nick/django-prod/uwsgi/uwsgi.sock
module=budget.wsgi
wsgi-file=/home/nick/django-prod/budget/wsgi.py
master=True
pidfile=/tmp/project-master.pid
vacuum=True
uid=www-data
gid=www-data
max-requests=5000
daemonize=/var/log/uwsgi/budget.log

I believe this may be less relevant but I'll include it here anyway, this is the custom widget I'm using as a date picker field which overrides the built-in DateField:

class JDatePickerWidget(forms.DateInput):
    def __init__(self, attrs=None, format='%m/%d/%Y'):
        super(JDatePickerWidget, self).__init__(attrs)
        self.format = format

    def render(self, name, value, attrs=None):
        if value is None:
            value = ''
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
        if value != '':
            # Only add the 'value' attribute if a value is non-empty.
            final_attrs['value'] = force_text(self._format_value(value))
        return_string = mark_safe(u'''<script type="text/javascript">
            $(function() { $( '#%s' ).datepicker({ dateFormat: 'mm/dd/yy'}); });
            </script>''' % final_attrs['id'])
        return_string = return_string + format_html('<input{0} />', flatatt(fin$
        return return_string

class JDatePicker(forms.DateField):
    widget = JDatePickerWidget

class JDatePickerField(models.DateField):
    def formfield(self, **kwargs):
        defaults = {'form_class': JDatePicker}
        defaults.update(kwargs)
        return super(JDatePickerField, self).formfield(**defaults)

How do I stop this caching from taking place so that the default field values will be refreshed when I revisit the page?

Upvotes: 2

Views: 1194

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

You didn't post your form's code so this is mostly a wild guess, but this kind of problem usually comes from initializing one of your form's fields with a call to datetime.datetime.now() (or to a Queryset or whatever). Since top-level code (which class-body level statements are parts of) is only executed once at import time, you have stale values.

(edit) From your comment below, your problem comes from using default=datetime.datetime.now() in your one of your model's fields. You want to pass the callable instead, ie default=datetime.datetime.now (no parens), as documented here: https://docs.djangoproject.com/en/1.6/ref/models/fields/#default

Upvotes: 4

Related Questions