Newlines replaced with HTML string in Django admin

I am trying to include some HTML templates in the default django change_form.html template for one of my models. My main goal is to create some diagrams in the Django backend.

The problem I am facing is that some newline lines continue to be replaced with <br />. This is especially troublesome because it breaks the JavaScript code that I'm really interested in, including on the page.

Currently, my approach uses readonly_fieldsModelAdmin in my class, combined with a function in my model that returns render_to_stringmy template.

My model

class Link(models.Model):
        user = models.ForeignKey(
            'auth.User',
            verbose_name=_('Author'),
            related_name="tinylinks",
        )

        link = models.CharField(
            max_length=2500,
            verbose_name=_('Link'),
        )

        clicks = models.PositiveIntegerField(
            default=0,
            verbose_name=_('Amount of views'),
        )

        def click_statistics(self):
            response = render_to_string(
                'admin/links/link/click_statistics.html', {})

ModelAdmin Class

class LinkAdmin(admin.ModelAdmin):
    list_display = ('link', 'user', 'clicks')
    search_fields = ['link', 'user']

    readonly_fields = ('click_statistics',)

    fieldsets = [
            ('Link', {'fields': ['user', 'link', 'clicks',]}),
            ('Statistics', {'fields': ['click_statistics',]}),
    ]

And that will be click_statistics.html

<table border="0" cellspacing="">
    <tr>
        <td valign="top">
            <div id='stat_line' class='stats_line line' style='display:block'>
                <script id="graphstat_line" type="text/javascript">
                    function graphstat_line() {
                        var data = google.visualization.arrayToDataTable([]);

                        var options = {
                            'legend': "none",
                            'pointSize': 3,
                            'theme': "maximized",
                            'curveType': "function",
                            'width': 430,
                            'height': 220,
                            'hAxis': {minTextSpacing: 80, maxTextLines: 1, maxAlternation: 1},
                            'vAxis': {minValue: -0.5, format: '#'},
                            'colors': ['#2a85b3']
                        }

                        new google.visualization.LineChart( document.getElementById('visualization_stat_line') ).draw( data, options );}
                        google.setOnLoadCallback( graphstat_line );
                </script>
                <div id="visualization_stat_line"></div>
            </div>
        </td>
    </tr>
</table>

I have the following questions:

  • <br />, render_to_string admin change_form.html?
  • ?
+4
1

django-suit? , suit/templates/admin/includes/fieldset.html:

    {# If multi-fields and wrap controls too #}
     {% if not singlefield %}
        <div class="multi-field-controls">
      {% endif %}

    {% if field.is_readonly %}
        <span class="readonly">{{ field|field_contents_foreign_linked|linebreaksbr }}</span>
    {% else %}
        {{ field.field }}
    {% endif %}

    {# For single field errors#}
    {% if singlefield and line.errors %}
        <span class="help-inline">{{ line.errors }}</span>
    {% endif %}

, INSTALLED_APPS , TEMPLATE_DIRS ( TEMPLATE_DIR/admin/includes/fieldset.html), |linebreaksbr .

, .

+3

Source: https://habr.com/ru/post/1541250/


All Articles