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.

OK - I'm outta my depth here. I've looked at the AdminTextareaWidget which is probably based on the TextInput built-in widget but see no way to influence what it does. In theory I'd create a new widget inheriting from that and use mark_safe() somehow.

Then I would need to tell the admin form to use it for my ddstructure field.

BUT see my template effort below


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)

Again, my feet don't touch bottom. I tried adding some code to change_form.html like this ...

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

{% block content %}{{ block.super }}
{% if original.ddstructure %}
    {{ original.ddstructure | safe }}
{% else %}
    <p>No 2D structure image </p>
{% endif %}

{% endblock %}

This kinda worked. It comes after block.super so it displays the svg image correctly but at the bottom of the page. AND the raw (or safe) code also displays in the location where I really want the image.

So close but ...

How do I get it to display inline in the proper place?

Many thanks for hanging in there

Cheers

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+unsubscr...@googlegroups.com <mailto: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 <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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1945816f-10f5-f940-9621-d1d03a6d5fb2%40dewhirst.com.au.

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to