darklow / django-suit

Modern theme for Django admin interface
http://djangosuit.com/
Other
2.33k stars 705 forks source link

How to use widget SuitSplitDateTimeWidget #371

Open ghost opened 9 years ago

ghost commented 9 years ago

How to use widget SuitSplitDateTimeWidget in intermediate page of admin action, show widget and icons fine, but, not show calendar for change date and time.

Thanks in advance...

darklow commented 9 years ago

So far i have used this widget only as a replacement for original SplitDateTimeWidget, never tried it using in other pages. Can you show me your code so far - the form and the view?

ghost commented 9 years ago

Thanks...

Yes, I use this widget as a replacement for original SplitDateTimeWidget too... only I show where code use

Admin action...

@admin.register(models.Caja)
class AdminCaja(admin.ModelAdmin):
....
    actions = [
        'add_pay',
    ]

Code action...

    def add_pay(self, request, queryset):
        template = 'admin/action_add_pay.html'
        form = None

        ...

        if not form:
            select = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
            form = forms.AdminAddPayForm(
                queryset, initial={'_selected_action': select})
            data = {
                'form': form,
            }
        return render_to_response(template, data,
            context_instance=RequestContext(request))
    add_pay.short_description = 'Agregar pagos'

and form code

    class AdminAddPayForm(forms.Form):
        _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)

        def __init__(self, objects, *args, **kwargs):
            super().__init__(*args, **kwargs)
            for obj in objects:

                ....

                date_real = forms.SplitDateTimeField(
                    label='Fecha real de pago', required=True, initial=now,
                    input_date_formats=['%d/%m/%y', '%d-%m-%y', '%y-%m-%d', '%y/%m/%d'])
                name_date_real = 'date_real_{}'.format(obj.id)
                self.fields[name_date_real] = date_real
                self.fields[name_date_real].widget = SuitSplitDateTimeWidget()

A image http://i.imgur.com/zjrP5Z7.png

Best regards...

SalahAdDin commented 9 years ago

Wow, interesting :D

ghost commented 8 years ago

It's possible, I used widget SuitSplitDateWidget in intermediate page of admin action?

Thanks

smaccona commented 8 years ago

I am having this problem too, but I reproduced it on a simple custom admin form instead of an intermediate page with latest Django Suit.

Django==1.8.6
django-suit==0.2.21

Here's my form & view:

from django import forms
from suit.widgets import SuitSplitDateTimeWidget
from django.shortcuts import render

class TestForm(forms.Form):
    test_date = forms.DateTimeField(widget=SuitSplitDateTimeWidget)

def test(request):
    form = TestForm()
    return render(request, 'admin/test.html', {'form': form})

In urls.py:

  ...
  url(r'^test$', admin.site.admin_view(test), name='test'),
  ...

Template test.html:

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

{% block content %}
{% load tz %}

<form method='post' action=''>
    {% csrf_token %}
    {{ form }}
</form>

{% endblock %}

When rendered, it looks exactly like the screenshot @mauriciobaeza posted - the calendar icon is obscured, and neither the hidden calendar icon nor the clock icon do anything when clicked on:

image

I see no errors from Django, and I see no issues in the Javascript console in my browser (latest Chrome for what it's worth). I don't have any good insights yet into what's happening here but I will dig into it.

smaccona commented 8 years ago

I have now noticed that something is screwed up with my dates & times even in the regular admin. For example, in the admin users page:

image

On the Suit demo site, it looks like this:

image

I have confirmed the versions of jQuery and Bootstrap that are getting loaded are identical in both cases. I will keep digging.

I just realized the Suit demo site overrides the User admin (see https://github.com/darklow/django-suit-examples/blob/master/admin.py#L282) so scratch that.

smaccona commented 8 years ago

I created a new virtualenv and a brand new Django app based on 1.8.6 with nothing but the default installation + latest Suit:

$ pip freeze
Django==1.8.6
django-suit==0.2.21

Ran initial migrations, created a superuser, added Suit to installed_apps, created a custom view as above and added it to the admin URLs. Screenshot was the same in Chrome & Firefox:

image

smaccona commented 8 years ago

Tried with latest Django as well:

$ pip freeze
Django==1.10.1
django-suit==0.2.21

Result was the same.

smaccona commented 8 years ago

I have not been able to quickly resolve this, and in the interests of expediency I just made my own MultiWidget which simply sets type="date" on the first input field and type="time" on the second. Since this is only being used in a custom Django admin form and all our admin users are using Chrome, this gives acceptable date & time input boxes which are not as pretty as the Django Suit widget but are serviceable.

image

Not ideal, and I would still love to see a resolution to this issue.

Here's my widget in case anyone needs it:

from django import forms
from suit.widgets import HTML5Input

class HTML5SplitDateTimeWidget(forms.SplitDateTimeWidget):
    def __init__(self, attrs=None):
        widgets = (HTML5Input(input_type='date'), HTML5Input(input_type='time'))
        forms.MultiWidget.__init__(self, widgets, attrs)
ghost commented 8 years ago

@smaccona work fine in normal form, but not work in intermediate page of admin action

Best regards