question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slow rendering on Django 1.7 if Layout objects

See original GitHub issue

Hello,

I’ve been using django crispy forms for quite a while now, and never noticed this issue until now. Maybe it comes from my syntax uising the Layout and Div object. I have DCF 1.4.0.

As soon as I’m using the layout.Layout object with some div inside, the time for rendering the form is extremely slow (1.5 second instead of 100ms).

I have a very simple form:

class TAFilterForm(forms.Form):
    subject = forms.CharField(required=False)
    ss = forms.ChoiceField(choices=models.SS_CHOICES, required=False)
    min_created_at = forms.DateField(required=False)
    max_created_at = forms.DateField(required=False)
    aa = forms.ChoiceField(choices=models.AA_CHOICES, required=False)
    cc = forms.ChoiceField(required=False)
    id = forms.CharField(required=False)

    def __init__(self, *args, **kwargs):
        super(TAFilterForm, self).__init__(*args, **kwargs)
        self.fields['cc'].choices = (('all', _('All')), )
        self.helper = helper.FormHelper()
        self.helper.form_method = 'GET'
        self.helper.add_input(
            layout.Submit('submit', _('Filter')),
        )
        self.helper.add_input(
            layout.Submit('csv', _('Get CSV')),
        )
        self.helper.layout = layout.Layout(
            layout.Div(
                layout.Div('id', css_class='span3'),
                layout.Div('ss', css_class='span3'),
                layout.Div('aa', css_class='span3'),
                layout.Div('cc', css_class='span3'),
                css_class='row-fluid filter-form-div',
            ),
            layout.Div(
                layout.Div('subject', css_class='span4'),
                layout.Div('min_created_at', css_class='span4'),
                layout.Div('max_created_at', css_class='span4'),
                css_class='row-fluid filter-form-div',
            ),
        )

thanks

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
rfj001commented, Aug 15, 2016

Using django’s cached template loader helps speed things up immensely.

Use this code in your settings.py file to use the cached template loader.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
            ],
            'loaders': [
                ('django.template.loaders.cached.Loader', [
                    'django.template.loaders.filesystem.Loader',
                    'django.template.loaders.app_directories.Loader',
                ]),
            ],
        },
    },
]

Without caching even without a Layout object crispy forms seems to take forever to render forms with many fields or formsets with many forms. By using the loader I brought a formset with 8 forms of 3 fields each from 10s rendering time down to 500ms on subsequent loads.

1reaction
carltongibsoncommented, Aug 15, 2016

It’s worthing looking at the FAQ, which discusses this too. http://django-crispy-forms.readthedocs.io/en/latest/faq.html?highlight=cache#how-fast-is-django-crispy-forms

I’m going to close this as I don’t think it’s an issue per se. (Happy to reconsider if enlightened suggestions arise.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django rendering a page with a large selection. Could this be ...
I render a .html page using django. The html page contains instances of artist . class Artist(EmptyModelBase): name = models.CharField( ...
Read more >
Fixed a slow render of a template - Django Forum
I was having a performance problems in my template. It was taking 15 seconds to render with 280 queries. Which seemed slow.
Read more >
Top 10 Mistakes That Django Developers Make - Toptal
In this tutorial, we will look at some common mistakes that are often made by Django developers and ways to avoid them. This...
Read more >
The Build — High Performance Django - Lincoln Loop
Building a high-traffic site from scratch is a dangerous undertaking. There are lots of places Django can be optimized and it's easy to...
Read more >
How to Optimize Django ORM Queries - Gearheart
Django ORM is a very abstract and flexible API. But if you do not know exactly how it works, you will likely end...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found