Friday, June 4, 2021

Re: SVG widget for the Admin

On 4/06/2021 5:15 pm, Derek wrote:
> Hi Mike
>
> The SVG is not going to display as SVG in a *form* - for that you'd
> need a widget, or perhaps just let users edit the text.

It will always be readonly.  Forever.

>
> Did you try displaying in the normal admin lists? This was my
> understanding of where you wanted to see it and the code snippets I
> posted were for that purpose.

I did try that and it did show the svg image - but that isn't where I
want it. It needs to be in the Admin displayed form.

>
> If you want to see it as "read only" in the form, but shown as SVG,

but shown as an image

> perhaps you need a custom model template
> (https://docs.djangoproject.com/en/dev/ref/contrib/admin/#templates-which-may-be-overridden-per-app-or-model)

I think that's where I need to go. Might be easier than a widget -
unless someone has built one already?

It would be quite handy to use mark_safe() somewhere ...

Thanks

Mike

>
> Derek
>
>
> On Thursday, 3 June 2021 at 08:22:57 UTC+2 Mike Dewhirst wrote:
>
> On 3/06/2021 4:00 pm, Derek wrote:
> > >
> > > 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",
> >     )
> >
>
> The above is unchanged.
>
> # admin
> class ChemicalAdmin(admin.ModelAdmin):
>
>     def _ddstructure(self, obj):
>         return mark_safe(obj.ddstructure)
>     ...
>     fieldsets = (
>         (
>             None,
>             {
>                 "fields": (
>                     "ddstructure",        # displays svg code
>                     "_ddstructure",
>                     ...
>             }
>     ),
>
> FYI it works perfectly as a column in the list display - but I don't
> need it there, rather in the page for the chemical.
>
> This is what happens if it is included in the fieldsets "fields"
> roster.
>
>
> FieldError at /admin/chemical/chemical/17/change/
>
> Unknown field(s) (_ddstructure) specified for Chemical. Check
> fields/fieldsets/exclude attributes of class ChemicalAdmin.
>
> Request Method: GET
> Request URL:
> http://localhost:8088/admin/chemical/chemical/17/change/
> <http://localhost:8088/admin/chemical/chemical/17/change/>
> Django Version: 3.2.3
> Exception Type: FieldError
> Exception Value:
>
> Unknown field(s) (_ddstructure) specified for Chemical. Check
> fields/fieldsets/exclude attributes of class ChemicalAdmin.
>
> Exception Location:
> D:\Users\mike\envs\xxai\lib\site-packages\django\contrib\admin\options.py,
>
> line 712, in get_form
> Python Executable: D:\Users\mike\envs\xxai\Scripts\python.exe
> Python Version: 3.8.3
>
>
> Cheers
>
> Mike
>
>
> > >
> > >     def _the_svg(self):
> > >         return """<svg width="100" height="100">%s</svg>""" %
> > > self.svg_text
> > >         _the_svg.allow_tags = True
> >
>
>
> --
> 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
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4862b67b-e5e9-40a3-b3aa-1666f4739703n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/4862b67b-e5e9-40a3-b3aa-1666f4739703n%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/ed050bc5-bbbb-0bba-ef03-eb2a26d71387%40dewhirst.com.au.

No comments:

Post a Comment