Finding a decent ordering widget ...

53 views
Skip to first unread message

Bernd Wechner

unread,
Mar 21, 2018, 5:54:35 AM3/21/18
to Django users
Hmmm, my next problem.

I envisage a widget which is kind of like a HTML textarea, but basically each line would hold the name of a field say, and I'd like a user to be able to order the lines, not change the text. Basically click anbd drag lines up and down to reposition them.

I've poked around for HTML widgets for a bit now and find my main handicap is I'm not even sure how to describe this in terse search terms.

I know I've seen such widgets in practice - I didn't invent the idea ;-) - but have till now never needed one and suddenly I'm left wondering if there is one out there somewhere, and/or how I'd go about finding one. No real desire to try and build one with javascript right now, not least given I'm confident it exists already and I just don't know what it's called, how to describe it well or find it and well, DRY, I don't like spending my time recreating widgets that exist already. Not a priority.

Does anyone here have any experience? Places tom look, terms to use in searching for one?

Regards,

Bernd.

Julio Biason

unread,
Mar 21, 2018, 8:14:54 AM3/21/18
to django...@googlegroups.com
Hi Bernd,

A bit of this "k anbd drag lines up and down to reposition them" is done by jQuery UI, in its "Draggable" functionality: https://jqueryui.com/draggable/#sortable

Not sure if that helps, though, 'cause you could provide functionality to reorder using plain JS, just reordering divs around moving the DOM around.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5ce7a37d-c5c1-4460-84a9-cb8974a86a6a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

Bernd Wechner

unread,
Mar 21, 2018, 8:06:27 PM3/21/18
to Django users
Well, I stayed up and hacked around and found a short JQuery option in the right direction:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script>
  $
(function() {
    $
("#sortable").sortable({cancel: false});
    $
("#sortable").disableSelection();
 
});
</script>

<div id="sortable">
     
<div><button type="button">Item 1</button></div>
     
<div><button type="button">Item 2</button></div>
     
<div><button type="button">Item 3</button></div>
     
<div><button type="button">Item 4</button></div>
     
<div><button type="button">Item 5</button></div>
     
<div><button type="button">Item 6</button></div>
     
<div><button type="button">Item 7</button></div>
</div>

Demo here:  https://jsfiddle.net/pLu54k47/29/

But I do remain curious if there's a Django widget implementation for that kind of a list too. Or in fact any other elegant easily used form of such a sortable list. Don't hesitate to share your experience if you have any.

Also I'd like to tidy this sample up a bit if I can to produce a self contained widget with only the to Jquery library dependencies perhaps I guess. Part of me wonders if there isn't or shouldn't be, or if I should try and start, a Django widget library with Django Widgets like this for reuse. Just musing.

Regards,

Bernd.

Bernd Wechner

unread,
Mar 22, 2018, 6:16:30 PM3/22/18
to Django users
Julio,

Funnily stumbled on the same notion myself last night. I've worked up a niceish sample here:

https://jsfiddle.net/34Lv6try/68/

So it's possible without too much hassle using JQuery Think I'll build be a widget out that fiddle ;-).

Regards,

Bernd.


On Wednesday, 21 March 2018 23:14:54 UTC+11, Julio Biason wrote:
Hi Bernd,

A bit of this "k anbd drag lines up and down to reposition them" is done by jQuery UI, in its "Draggable" functionality: https://jqueryui.com/draggable/#sortable

Not sure if that helps, though, 'cause you could provide functionality to reorder using plain JS, just reordering divs around moving the DOM around.
On Wed, Mar 21, 2018 at 6:54 AM, Bernd Wechner <bernd....@gmail.com> wrote:
Hmmm, my next problem.

I envisage a widget which is kind of like a HTML textarea, but basically each line would hold the name of a field say, and I'd like a user to be able to order the lines, not change the text. Basically click anbd drag lines up and down to reposition them.

I've poked around for HTML widgets for a bit now and find my main handicap is I'm not even sure how to describe this in terse search terms.

I know I've seen such widgets in practice - I didn't invent the idea ;-) - but have till now never needed one and suddenly I'm left wondering if there is one out there somewhere, and/or how I'd go about finding one. No real desire to try and build one with javascript right now, not least given I'm confident it exists already and I just don't know what it's called, how to describe it well or find it and well, DRY, I don't like spending my time recreating widgets that exist already. Not a priority.

Does anyone here have any experience? Places tom look, terms to use in searching for one?

Regards,

Bernd.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Bernd Wechner

unread,
May 1, 2018, 9:14:39 AM5/1/18
to django...@googlegroups.com
This interests me:

    https://teamtreehouse.com/community/django-how-can-i-retrieve-a-value-from-the-middleware

alas no answer there, and time has changed things.

I have checked form experience, that stuff added to response.context_data in middleware is not seen in templates (perhaps comes too late!).

Another pager here:

    https://mlvin.xyz/django-templates-context-processors.html

describes how to add to context used context processors. Searching the web is nightmare in this space because of how Django has changed over time and the different methods in different versions.

The problem I have with that sample above is he's replicating code by including in settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates"),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                # our custom context processor
                'utils.context_processors.active_shows',
            ],
        },
    },
]

most of which is standard. Is there not a much more DRY way to add a context processor? Would this work?

TEMPLATES = [
    {
        'OPTIONS': {
            'context_processors': [
                'utils.context_processors.active_shows',
            ],
        },
    },
]

Probably not at a guess.  But is there a nicer way to do it?

Another way might be to patch the response.content in the middleware but that too seems messy.

Surely there's a nice way for middlware to take some timing stats around the:

        response = self.get_response(request)

invocation that typically returns a response (and which I presume has already been through the whole template processing and has finalised response.content by the time it's done, so taking a timestamp after it's done is easy but making the result available in the templates via a context variable like {{execution_time}} would seem structurally impossible if context processing has already happened.

I can only imagine a trick using another context key like %%execution_time%% say and replacing that response.content.

I wonder if I'm on the right track here? Or if someone has a better idea?

Regards,

Bernd.


Reply all
Reply to author
Forward
0 new messages