Wednesday, June 2, 2021

Re: SVG widget for the Admin

Derek

I pretty much tried what you suggested. I dont have access to my machine atm so that's all I can really say. 

I looked at the link and understand the list customisation because I already use it elsewhere. 

It does look like I'll have to switch in my own template so I can include a safe filter.

Thanks again

Mike



--
(Unsigned mail from my phone)



-------- Original message --------
From: Derek <gamesbook@gmail.com>
Date: 2/6/21 23:39 (GMT+10:00)
To: Django users <django-users@googlegroups.com>
Subject: Re: SVG widget for the Admin

When you say "I tried your suggestion and had no success." what exactly did you try  - i.e. what code did you use and what errors did you get?

WRT "Not sure where this should live for the Admin to call it and display the image." - it lives exactly as I showed it in my example; its a user-defined property for the model in which the SVG text is stored.  You can use it in the admin as you would the normal model fields. 

There is a good example of defining and using your own derived 'field'  here:
https://realpython.com/customize-django-admin-python/#modifying-a-change-list-using-list_display

(Please note that to show HTML strings, you need to use the format_html() method. Ignore mt previous reference to "allow_tags" as this is deprecated.)

Derek


On Wednesday, 2 June 2021 at 10:05:11 UTC+2 Mike Dewhirst wrote:
On 1/06/2021 11:44 pm, Derek wrote:
> You haven't defined where in the admin interface you want this image
> to be displayed?  A table?
>
> I have not tried this, but could you not create an additional field on
> your model that returns the HTML-encoding needed to display the SVG?

Derek, thanks for responding. I tried your suggestion and had no success.

Where in the admin? Just among a normal roster of fields. The svg is a
molecular structure and the image needs to appear for example just after
the molecular formula. I collect the actual svg code via a public API on
saving the record.

>
> E.g.
>
> class MyModel():
>     svg_text = CharField()

in my case it is ...

class chemical(models.Model):
    ...
    ddstructure = models.TextField(
        null=True,
        blank=True,
        verbose_name="2D structure",
    )

>
>     def _the_svg(self):
>         return """<svg width="100" height="100">%s</svg>""" %
> self.svg_text
>         _the_svg.allow_tags = True

Not sure where this should live for the Admin to call it and display the
image.


> I am not sure about editing visually, however - I would expect that
> would require a specialised widget.

It is read-only in all cases.

I was thinking a widget might be needed just for display. The svg code
arrives complete with tags so really all it needs is a mechanism to
persuade the admin it is safe to render as is.

I haven't seen such a (probably insecure) "feature" previously. I've
looked through the docs but haven't found it yet.

Thanks again

Mike


>
> HTH
> Derek
>
>
> On Tuesday, 1 June 2021 at 03:28:59 UTC+2 Mike Dewhirst wrote:
>
> I collect the svg source for an image from a public API and store
> it in
> a models.TextField. I have no difficulty displaying it in a normal
> view
> and my own template. Nothing special, it just emerges. I don't
> even need
> a 'safe' filter.
>
> However, I really want to display such images in the Admin. At this
> stage all it displays is the svg source.
>
> What is the correct way to make the image appear in the Admin?
>
> Do I need a special field inheriting from TextField? Do I need a
> special
> widget? Is there a way to mark admin field values as safe?
>
> Thanks for any hints
>
> Mike
>
> --
> Signed email is an absolute defence against phishing. This email has
> been signed with my private key. If you import my public key you can
> automatically decrypt my signature and be sure it came from me. Just
> ask and I'll send it to you. Your email software can handle signing.
>
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/520bf296-ec68-48ad-8fe2-f106823efac2n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/520bf296-ec68-48ad-8fe2-f106823efac2n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/518a9b56-de5a-46b8-8578-89bd99f20f4fn%40googlegroups.com.

No comments:

Post a Comment