Manually ordering a column in Django admin is not working on remote server but is ok locally

Question:

In Django admin, I can order each table according to whatever column I want, without having to specify anything in the model:

enter image description here

A little arrow describing the ordering is normally appearing after clicking on a column header.

admin.py:

from django.contrib import admin
from .models import User
from .resources import UserAdminResource
from import_export.admin import ExportActionModelAdmin

class UserAdmin(ExportActionModelAdmin, admin.ModelAdmin):
    resource_class = UserAdminResource
    list_display = [f.name for f in User._meta.fields]
    list_display.remove("id")

admin.site.register(User, UserAdmin)

resources.py:

from import_export import resources
from .models import User

class UserAdminResource(resources.ModelResource):
    class Meta:
        model = User

models.py:

from django.db import models
from django.conf import settings

class User(models.Model):
    name = models.CharField(
        max_length=256, verbose_name=_("Name"), null=False
    )
    address = models.CharField(
        max_length=256, verbose_name=_("Address"), null=True, blank=True
    )
    city = models.CharField(
        max_length=256, verbose_name=_("City"), null=True, blank=True
    )

    class Meta:
        verbose_name = _("User")
        verbose_name_plural = _("Users")

    def __str__(self):
        return self.name

But when deployed on a remote server, clicking on the column header makes it quickly blink, but it’s not working (i.e. it’s not ordering the rows), even so the URL changes to: server.org/admin/lorem/user/?o=2 for example.

Is there anything special to do when one want to sort columns in the Django admin on a remote server compared to a local machine?

Both the local and remote deployment are on the same branch with the same environment (docker).

The one and only difference I spotted was this extra first line in the network manager of my web browser when I click the column header:

enter image description here

Edits

I’ve noticed something important:

The ordering by clicking on column headers is working well on the remote server when the port is specified within the URL: server.org:80/admin/lorem/user/?o=2 but not at server.org/admin/lorem/user/?o=2 (nginx is working on the server for redirecting traffic, so maybe I have to see there actually…).

Django version 4.1.2 / Python 3.9.14

Asked By: s.k

||

Answers:

As I recently noticed by doing a wrong manipulation, that the ordering is actually working on the remote server by hitting the URL when the original port is specified, e.g. server.org:80/admin/lorem/user/?o=2 and that it’s not the case when the port is not provided, I suspected a badly written rule in my Nginx configuration.

And it was indeed the case.

According to: https://stackoverflow.com/a/8130872/6630397 one has to manually build the full URL.

So I updated this:

    location ~ ^/(.*)$ {
        include proxy_params;
        proxy_pass http://127.0.0.1:81/$1;
    }

to this:

    location ~ ^/(.*)$ {
        include proxy_params;
        proxy_pass http://127.0.0.1:81/$1$is_args$args;
    }

And all of a sudden, it works.

I thought that $1 was actually catching everything up to the end of the URL, not matter if query parameters are present or not. But it seems it doesn’t work this way.

More: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Answered By: s.k
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.