skip to Main Content

I’m trying to deploy my django application in a Droplet virtual machine (DigitalOcean) following this guide.

For this purpose I’ve used nginx and gunicorn with success.

The problem I’m facing is with django-select2 and is that the widget of the form always show ‘Results cannot be found.’ giving a 404 error, while in my local environment it work flawlessly!

I’ve tried to implementing the cache (hoping that it will solve the issue) but things get worse since with the cache enabled the widget seems to not work anymore even in local.

Now the time to show my code so far:

settings.py:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    },
    'select2': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 60 * 60 * 24,
    },
}

SELECT2_CACHE_BACKEND = 'select2'

This is the code done following the guide displayed here

PS: Note that using redis instead of memcached give the same result

this is my nginx configuration:

server {
    listen 80;
    server_name 46.101.134.225;
    client_max_body_size 2M;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static {
        root /projectname;
    }

    location /media {
        root /projectname/staticfiles;
    }

    location /assets {
        root /;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/projectname/projectname.sock;
    }
}

this is my gunicorn configuration:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/projectname
ExecStart=/projectname/bin/gunicorn --access-logfile - --workers 3 --bind unix:/projectname/projectname.sock projectname.wsgi:application

[Install]
WantedBy=multi-user.target

this is my form with the widgets:

from django_select2.forms import ModelSelect2Widget, ModelSelect2MultipleWidget
...
class CompanyForm(BaseModeratedObjectForm):
    ...
    city = forms.ModelChoiceField(widget=ModelSelect2Widget(
        model=City, search_fields=['name__istartswith']
    ), queryset=City.objects.all(), required=False)
    ...
    treatments = forms.ModelMultipleChoiceField(widget=ModelSelect2MultipleWidget(
        model=Treatment, search_fields=['name__icontains']
    ), queryset=Treatment.objects.all(), required=False)
    ...

PS: Note that before implementing the cache at all i obtained this results:

  • Working perfectly in my local environment.
  • Working sometimes in my production environment (sometimes data appear while typing).

After implementing the cache nothing work anymore (in my local environment manually navigating to the select2 url i can see that it display the error field_id not found

EDIT:

As my answer was accepted even in the django-select2 repo on github i’ve done a pull request on the project changing the documentation to make notice of this case!

3

Answers


  1. Chosen as BEST ANSWER

    I finally manage to solve this issue, the problem was that, when implementing the redis cache, the system was not able to find the redis server.. Installing it with sudo apt-get install redis-server solved the issue, alongside using the redis cache!


  2. Have also installed redis but the solution might be the TIMEOUT setting in settings.py. Not sure though

    Login or Signup to reply.
  3. Adding a comment for anyone else struggling with this. This seems to be caused by issues in your cache backend. Make sure that you’re not using the locmem cache in production or the dummy cache. I solved this by moving to the database cache:

    CACHES = {
    'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        },
    'select2': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'select2_cache_table',
        }
    }
    SELECT2_CACHE_BACKEND: str = 'select2'
    

    Check this link for more options on cache backends. Make sure you follow all of the steps to properly configure the cache.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search