silentsokolov / django-admin-rangefilter

A Django app that lets you filter data by date range and numeric range in the admin UI
MIT License
713 stars 106 forks source link

Icon is not displayed #21

Closed aszubarev closed 5 years ago

aszubarev commented 5 years ago

calendar icon is not displayed in admin panel for DateRangeFilter and DateTimeRangeFilter

django==2.1.2 django-admin-rangefilter==0.3.8

2018-10-29 18 05 24
silentsokolov commented 5 years ago

Please, check calendar icon on "edit page", like /admin/APP_NAME/MODEL_NAME/ID/change/:

2018-10-29 22 24 01

are they displayed?

aszubarev commented 5 years ago

Yes, I checked it on user admin page

2018-10-30 11 21 32
aszubarev commented 5 years ago

i compared html on "edit page" and on filter

edit page

2018-10-30 11 24 57

filter

2018-10-30 11 29 07
silentsokolov commented 5 years ago

I could not reproduce the error. The following code probably does not work correctly. You are using custom Admin/Form classes?

aszubarev commented 5 years ago

I deleted the cache from the browser and it all worked!

silentsokolov commented 5 years ago

What did you do before that? Update django 2.0 -> 2.1? switch venv?

aszubarev commented 5 years ago

Yes. If you use django == 2.0.9, then everything works stably. When using version 2.1.2, the icon is displayed only if the page is reloaded without a cache. However, with a simple reboot, the icon is not displayed again. All these actions are carried out in the docker container, static files are given Nginx after the "collectstatic" command. If you run a project from IDEA in debug mode, then even with version 2.1.2 everything works stably.

aszubarev commented 5 years ago

Added class Media. It worked for me. Now, when i just reload the page the icon is always displayed

class ScheduleAdmin(admin.ModelAdmin):
    list_filter = [
        ('reception_start', DateRangeFilter)
    ]

    class Media:
        js = DateRangeFilter.get_js()
silentsokolov commented 5 years ago

Error in JS console?

silentsokolov commented 5 years ago

Run with docker:

FROM python:3.6

RUN apt-get update && apt-get install -y \
        nginx \
        sqlite3 \
        gettext \
    --no-install-recommends && rm -rf /var/lib/apt/lists/*

COPY etc/nginx-app.conf /etc/nginx/sites-available/default

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app

RUN python manage.py collectstatic --noinput

EXPOSE 80

CMD ["sh", "-c", "service nginx start && python manage.py runserver --nostatic"]
server {
   listen 80;
   server_name host.com;

   location / {
       try_files $uri $uri/index.html @backend;
       root /usr/src/front;
   }

   location ^~ /static/ {
       alias /usr/src/static/;
   }

   location ^~ /data/ {
       alias /usr/src/data/;
   }

   location @backend {
       proxy_pass http://localhost:8000;
       proxy_redirect off;
       proxy_set_header   Host $host;
       proxy_set_header   X-Real-IP $remote_addr;
       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header   X-Forwarded-Host $server_name;
   }
}

docker build -t foo . && docker run --name="bar" --rm -it -p 9000:80 foo

With cache / out cache - works (((

aszubarev commented 5 years ago

OK. I think the problem is somewhere in my code. but it is very strange. I solved this problem by adding the Media class and I like this solution. I suggest to close the issue

silentsokolov commented 5 years ago

Here the reason why we refused this solution.

Thank. I will not close the issue, because the problem is serious, but need more information

silentsokolov commented 5 years ago

@ZubAnt please,

$(".datetimeshortcuts").remove();
DateTimeShortcuts.init();

If possible, paste the following code into 105-106 lines

aszubarev commented 5 years ago

i am install from here https://github.com/ZubAnt/django-admin-rangefilter/commit/99a46070ed5e9b333353d55af55f7955c77add7c

Uncaught TypeError: $ is not a function
    at HTMLDocument.<anonymous> ((index):277)
    at l (jquery.min.js:2)
    at c (jquery.min.js:2)
2018-10-30 16 04 33
silentsokolov commented 5 years ago

Ok, big thx!

silentsokolov commented 5 years ago

Oh, i know is code not best, this can help.

Replace block here to:

   django.jQuery('document').ready(function () {
        if (!('DateTimeShortcuts' in window)) {
            django.jQuery.when(
                {% for m in spec.form.js %}
                    django.jQuery.getScript('{{m}}'),
                {% endfor %}
                django.jQuery.Deferred(function( deferred ){
                    django.jQuery( deferred.resolve );
                })
            ).done(function(){
                django.jQuery('.datetimeshortcuts').remove();
                DateTimeShortcuts.init();
            });
        }
    });

If you encounter the same problem please comment here

aszubarev commented 5 years ago

It work with/out cache! https://github.com/ZubAnt/django-admin-rangefilter/commit/4b48c94c7e2b2c6675b3e364cfb87e2a70c36379

qulc commented 5 years ago

is not resolved.

Django                   2.1.4
django-admin-rangefilter 0.3.10

calendar.js DateTimeShortcuts.js is not loaded.

image

qulc commented 5 years ago

Try ignore DJANGO_RANGEFILTER_ADMIN_JS_SET always get_js() https://github.com/silentsokolov/django-admin-rangefilter/blob/master/rangefilter/filter.py#L110

class DatetimeRangeFilter(DateTimeRangeFilter):
    def get_form(self, request):
        form_class = super().get_form(request)
        form_class.js = super().get_js()
        return form_class

Now it worked for me.

image

silentsokolov commented 5 years ago

Please, show admin.py. You use default admin.ModelAdmin without overrides?

qulc commented 5 years ago

I overrides change_list method in admin.ModelAdmin

Now without overrides is worked.

denisvolokh commented 5 years ago

Hi,

I have the same problem with the overriding change_list template.

If I override change_list template date range component stop working and here is the error from console:

image

silentsokolov commented 5 years ago

try reimport jQuery.js

denisvolokh commented 5 years ago

Did not work. Here is my template:

{% extends "admin/change_list.html" %}

{% load staticfiles %}

{% block extrahead %}

<script src="https://code.highcharts.com/highcharts.src.js"></script>

<script>
    window.onload = function () {
        console.log("[+] Window onLoad!")

        Highcharts.chart("chart", {{chart|safe}});
    }
</script>

{% endblock %}

{% block content %}

Additional content goes here
{{ block.super }}

{% endblock %}

silentsokolov commented 5 years ago

jQuery != django.query, try import init.js

denisvolokh commented 5 years ago

Tried to load jquery and jquery.ini like:

{% block extrahead %}

<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>

<script src="https://code.highcharts.com/highcharts.src.js"></script>

<script>
    window.onload = function () {
        console.log("[+] Window onLoad!")

        Highcharts.chart("chart", {{chart|safe}});
    }
</script>

{% endblock %}

Now getting this error:

image

VictorIsra commented 4 years ago

Django==2.2.12 django-admin-rangefilter==0.6.1

admin.py:

from rangefilter.filter import DateRangeFilter, DateTimeRangeFilter
from .models import Event

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
    list_filter = (
        ('date', DateRangeFilter), ('date', DateTimeRangeFilter),
    )

errorr

silentsokolov commented 4 years ago

@VictorIsra Hello, Grapelli != Django. We dont support it.

VictorIsra commented 4 years ago

Hello! oh, too bad. Do you have any perspective of adding Grapelli support? this project is pretty similar and works on Grapelli, but it has an unusual alphabet:

https://pypi.org/project/django-admin-rangefilter-jalali/

silentsokolov commented 4 years ago

Hello! oh, too bad. Do you have any perspective of adding Grapelli support?

I dont think so. ...

PR welcome!