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.

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.

If you want to see it as "read only" in the form, but shown as SVG, 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)

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/
> 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+unsubscr...@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.

Reply via email to